Link to Question: https://cses.fi/problemset/task/1643
Please find my code below. But essentially I have long long maxSum value that initially equals to smallest long value. I also have a int minValue that is initially set to 0. I then loop through each element in the input, and create a prefix value for that. I then subtract that prefix value by minValue, I then check if that is greater than the maximum sum so far, if it is I update the maximum sum variable. Next if the current prefix value is smaller than the minValue, I update minValue to that prefix sum value. Now my code works for 11/14 cases. Its not passing Case 8,9, 10. I’m not sure what I did wrong, please let me know:
#include <iostream>
#include <climits>
using namespace std;
int N, temp;
long long maxSum = LONG_MIN;
int minValue = 0; // Must start at 0
int main(){
cin >> N;
long long prefixSum[N+1];
prefixSum[0] = 0;
for (int i = 1; i<=N; i++){
cin >> temp;
prefixSum[i] = prefixSum[i-1] + temp;
maxSum = max(maxSum, prefixSum[i] - minValue);
if (prefixSum[i] < minValue){
minValue = prefixSum[i];
}
}
cout << maxSum;
}
Thanks!