Used a trivial approach for solving using dp which would iterate twice through the vector. I can’t find what’s causing SIGABRT in this though
#include <bits/stdc++.h>
using namespace std;
long long int solver(int n){
if (n == 0) return 0;
vector monster;
long long int s;
for (int i = 0; i < n; i++) {
cin >> s;
monster.push_back(s);
}
if (n == 1) return monster[0];
long long int totalmax = max(monster[0], monster[1]);
vector maxvec{monster[0], monster[1]};
for (int i = 2; i < n; i ++) {
long long int tempmax = 0;
for (int j = 0; j < i-1; j++) if (tempmax < maxvec[j]) tempmax = maxvec[j];
maxvec[i] = tempmax + monster[i];
if (totalmax < maxvec[i]) totalmax = maxvec[i];
}
return totalmax;
}
int main() {
int t;
cin >> t;
for (int i = 1; i <= t; i++){
int n;
cin >> n;
long long int res = solver(n);
cout << "Case " << i << ": " << res << endl;
}
}
created
last reply
- 3
replies
- 785
views
- 2
users