Code Submission Evaluation System Login

HIIT Open 2018

Start:2018-05-26 11:00:00
End:2018-05-26 16:00:00
 

Tasks | Messages | Scoreboard | Statistics


CSES - HIIT Open 2018 - Results
History
2018-05-26 12:00:53
Task:Buy Low, Sell High
Sender:Tefyn virallinen maajoukkue
Submission time:2018-05-26 12:00:53
Language:C++
Status:READY
Result:ACCEPTED

Test results

testverdicttime (s)
#1ACCEPTED0.12 / 1.00details
#2ACCEPTED0.21 / 1.00details
#3ACCEPTED0.19 / 1.00details
#4ACCEPTED0.26 / 1.00details
#5ACCEPTED0.27 / 1.00details
#6ACCEPTED0.01 / 1.00details
#7ACCEPTED0.01 / 1.00details
#8ACCEPTED0.01 / 1.00details
#9ACCEPTED0.12 / 1.00details

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 ...
view   save

correct output
0
view   save

user output
0
view   save

Test 2

Verdict: ACCEPTED

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

correct output
499999
view   save

user output
499999
view   save

Test 3

Verdict: ACCEPTED

input
500000
500000 499999 499998 499997 49...
view   save

correct output
0
view   save

user output
0
view   save

Test 4

Verdict: ACCEPTED

input
500000
617752857 533265574 365848360 ...
view   save

correct output
1999980408
view   save

user output
1999980408
view   save

Test 5

Verdict: ACCEPTED

input
500000
209620375 316066031 756114325 ...
view   save

correct output
1999992655
view   save

user output
1999992655
view   save

Test 6

Verdict: ACCEPTED

input
1
1
view   save

correct output
0
view   save

user output
0
view   save

Test 7

Verdict: ACCEPTED

input
2
1 1
view   save

correct output
0
view   save

user output
0
view   save

Test 8

Verdict: ACCEPTED

input
2
2 1
view   save

correct output
0
view   save

user output
0
view   save

Test 9

Verdict: ACCEPTED

input
500000
1 1000000000 2 2 2 2 2 2 2 2 2...
view   save

correct output
1999999998
view   save

user output
1999999998
view   save