'E-olymp: Profit. 90% gives, one test wrong answer

Here the full exercise

Description of my code: For example, take n = 3, and let p [i] be 1 2 3. For beginnings, max = 0. I take in this order the sequences: 1, 1 + 2, 1 + 2 + 3, 2, 2 + 3, 3 and every time I find the amount of some kind, I compare it with the max amount.

My code is this:

#include <iostream>
using namespace std;
//f - number of all sequences(sequence length 1 to n)
//n - number of p[i]
//q is sequence sum
//x this is the number that increases when we get to the last element. For example, the numbers 1 2 3, when we reach 1 + 2 + 3, then x grows and go to 2, 2 + 3
int main()
{
    int n, i, f = 0, q, x = 0, max, j; 
    cin >> n;
    int *p = new int[n]();
    for (i = 0; i < n; i++, f += i) {
        cin >> p[i];
    }
    max = p[0];
    for (i = 0 ; i < f; x++ ) {
        q = 0;
         for (j = x; j < n; j++,i++) {
             q += p[j];
             if (q >= max) {
                 max = q;
             }
         }
    }
    cout <<max;
    return 0;
}

And this is 90% result:

And this is 90% result



Solution 1:[1]

I suspect the profit may be negative, in this case the algorithm should pick the least bad day, since zero days are not allowed.

Initialize max to INT_MIN.

Solution 2:[2]

Every time you entered "Pi", check if sum + Pi is positive or not.
If it is not then sum = 0
Else update sum to sum + Pi
Update ans

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
const int INF = 1e6;

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n; cin>>n;
    int mx = -INF;
    int sum = 0;
    for(int i = 1;i<=n;i++){
        int x; cin>>x;
        if(sum + x > 0){
            sum += x;
        }
        else{
            sum = 0;
        }
        mx = max(mx,sum);
    }
    cout << mx << "\n";
    return 0;
}

This should work

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex Guteniev
Solution 2 HaciyevAlik