CSES - HIIT Open 2018 - Results
Submission details
Task:Buy Low, Sell High
Sender:Tefyn virallinen maajoukkue
Submission time:2018-05-26 12:00:53 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.12 sdetails
#2ACCEPTED0.21 sdetails
#3ACCEPTED0.19 sdetails
#4ACCEPTED0.26 sdetails
#5ACCEPTED0.27 sdetails
#6ACCEPTED0.01 sdetails
#7ACCEPTED0.01 sdetails
#8ACCEPTED0.01 sdetails
#9ACCEPTED0.12 sdetails

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:17:13: warning: unused variable 'ans2' [-Wunused-variable]
     int64_t ans2 = 0;
             ^~~~

Code

#include <bits/stdc++.h>

using namespace std;
int64_t mi[505050], im[505050], ma[505050], am[505050], a1[505050], a2[505050];
int main(){
    int n; cin >> n;
    vector<int64_t> p;
    for(int i = 0; i < n; ++i){
        int64_t tmp; cin >> tmp;
        p.push_back(tmp);
    }
    ma[0] = p[0];
    mi[0] = p[0];
    am[n-1] = p[n-1];
    im[n-1] = p[n-1];
    int64_t ans1 = 0;
    int64_t ans2 = 0;
    for(int i = 1; i < n; ++i){
        ma[i] = max(ma[i-1], p[i]);
        mi[i] = min(mi[i-1], p[i]);
    }
    for(int i = n-2; i >= 0; --i){
        am[i] = max(am[i+1], p[i]);
        im[i] = min(im[i+1], p[i]);
    }
    //cout << "a1 ";
    for(int i = 0; i < n; ++i){
        a1[i] = p[i]-mi[i];
        if(i > 0){
            a1[i] = max(a1[i], a1[i-1]+ma[i]-ma[i-1]);
        }
       // cout << a1[i] << " ";
    }
    //cout << endl;
    //cout << "a2 ";
    for(int i = n-1; i > 0; --i){
        a2[i] = am[i]-p[i];
        if(i < n-2){
            a2[i] = max(a2[i], a2[i+1]+im[i+1]-im[i]);
            
        }
        //cout << a2[i] << " ";
    }
   // cout << endl;
    ans1 = max(a1[n-1], a2[0]);
    for(int i = 0; i < n-1; ++i){
        ans1 = max(ans1, a1[i]+a2[i+1]);
    }
    cout << ans1 << endl;
    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
500000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
0

user output
0

Test 2

Verdict: ACCEPTED

input
500000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
499999

user output
499999

Test 3

Verdict: ACCEPTED

input
500000
500000 499999 499998 499997 49...

correct output
0

user output
0

Test 4

Verdict: ACCEPTED

input
500000
617752857 533265574 365848360 ...

correct output
1999980408

user output
1999980408

Test 5

Verdict: ACCEPTED

input
500000
209620375 316066031 756114325 ...

correct output
1999992655

user output
1999992655

Test 6

Verdict: ACCEPTED

input
1
1

correct output
0

user output
0

Test 7

Verdict: ACCEPTED

input
2
1 1

correct output
0

user output
0

Test 8

Verdict: ACCEPTED

input
2
2 1

correct output
0

user output
0

Test 9

Verdict: ACCEPTED

input
500000
1 1000000000 2 2 2 2 2 2 2 2 2...

correct output
1999999998

user output
1999999998