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 11:57:03
2018-05-26 11:40:04
Task:Buy Low, Sell High
Sender:Wave of Technology
Submission time:2018-05-26 11:57:03
Status:READY
Result:ACCEPTED

Show test data

Code

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;



int main() {

  cin.tie(NULL);
  std::ios::sync_with_stdio(false);

  ll n;
  cin >> n;
  vector<ll> inputti(n+5);
  vector<ll> leftmin(n+5);
  vector<ll> rightmax(n+5);
  vector<ll> leftbest(n+5);
  vector<ll> rightbest(n+5);


  for(int i = 0; i < n; i++)
    cin>>inputti[i+1];



  // compute leftmin
  leftmin[1] = inputti[1];
    for(int i = 2; i <= n; i++)
        leftmin[i] = min(inputti[i],leftmin[i-1]);

    rightmax[n] = inputti[n];
    // compute rightmax
    for(int i = n-1; i >= 1; i--)
        rightmax[i] = max(inputti[i],rightmax[i+1]);

// compute leftbest
    leftbest[0] = 0;
    for(int i = 1; i <= n; i++)
        leftbest[i] = max(inputti[i]-leftmin[i],leftbest[i-1]);


  // compute rightbest
    rightbest[n+1] = 0;
    for(int i = n; i >= 1; i--)
        rightbest[i] = max(-inputti[i]+rightmax[i],rightbest[i+1]);


    ll outcome = 0;
    for(int i = n-1; i >= 1; i--)
        outcome = max(outcome,leftbest[i]+rightbest[i]);

    outcome = max(outcome, rightbest[1]);
    cout<<outcome<<endl;

}