Code Submission Evaluation System Login

CSES - HIIT Open 2018

HIIT Open 2018

Contest start:2018-05-26 11:00:00
Contest end:2018-05-26 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard | Statistics


History
2018-05-26 12:00:53
Task:Buy Low, Sell High
Sender:Tefyn virallinen maajoukkue
Submission time:2018-05-26 12:00:53
Status:READY
Result:ACCEPTED

Show test data

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;
}