Submission details
Task:Maximum sum
Sender:ariadna.roga
Submission time:2025-09-15 17:31:08 +0300
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.04 sdetails
#7ACCEPTED0.05 sdetails
#80.10 sdetails
#90.10 sdetails
#100.10 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.01 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails

Code

#include <iostream>
#include <vector>

using namespace std;

int main() {
    long long n;
    cin >> n;

    // Read the values
    vector<long long> v(n);
    for (long long i = 0; i < n; ++i)
        cin >> v[i];

    vector<long long> best(n, 0);
    vector<long long> acc(n, 0);

    // Find the first positive or the highest negative
    long long first = 0;
    long long highest_negative;
    while (v[first] < 0 and first < n)
    {
        if (first == 0)
            highest_negative = v[first];
        else if (v[first] > highest_negative)
            highest_negative = v[first];
        ++first;
    }

    // All negative numbers
    if (first == n)
        cout << highest_negative << endl;
    // Some positive number(s)
    else {
        best[first] = v[first];
        acc[first] = v[first];

        for (long long i = first+1; i < n; ++i) {
            if (v[i] < 0) { // Always better not to pick it
                best[i] = best[i - 1];
                acc[i] = best[i-1] + v[i];
            }
            else {
                if (best[i-1] > (acc[i-1] + v[i]) and best[i-1] > v[i]) {
                    best[i] = best[i - 1];
                    acc[i] = acc[i - 1] + v[i];
                }
                else if ((acc[i-1] + v[i]) > v[i]) {
                    best[i] = acc[i - 1] + v[i];
                    acc[i] = best[i];
                }
                else {
                    best[i] = v[i];
                    acc[i] = v[i];
                }
            }
        }

        cout << best[n - 1] << endl;
    }
}

Test details

Test 1

Verdict: ACCEPTED

input
10
1 1 1 1 1 1 1 1 1 1

correct output
10

user output
10

Test 2

Verdict: ACCEPTED

input
10
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

correct output
-1

user output
-1

Test 3

Verdict: ACCEPTED

input
10
24 7 -27 17 -67 65 -23 58 85 -...

correct output
185

user output
185

Test 4

Verdict: ACCEPTED

input
10
99 -59 31 83 -79 64 -20 -87 40...

correct output
154

user output
154

Test 5

Verdict: ACCEPTED

input
10
-19 61 60 33 67 19 -8 92 59 -3...

correct output
383

user output
383

Test 6

Verdict: ACCEPTED

input
200000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
200000

user output
200000

Test 7

Verdict: ACCEPTED

input
200000
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...

correct output
-1

user output
-1

Test 8

Verdict:

input
200000
381082742 830199996 -85684827 ...

correct output
231210956017

user output
29607320566291

Test 9

Verdict:

input
200000
-935928962 -795492223 75287481...

correct output
184607318819

user output
29621770658832

Test 10

Verdict:

input
200000
524408131 613017181 -62281009 ...

correct output
360019999220

user output
29794715810520

Test 11

Verdict: ACCEPTED

input
1
1

correct output
1

user output
1

Test 12

Verdict: ACCEPTED

input
1
-2

correct output
-2

user output
-2

Test 13

Verdict: ACCEPTED

input
5
-1 -1 -1 -1 -2

correct output
-1

user output
-1

Test 14

Verdict: ACCEPTED

input
2
-3 -2

correct output
-2

user output
-2

Test 15

Verdict: ACCEPTED

input
1
-1000000000

correct output
-1000000000

user output
-1000000000