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 13:33:58
2018-05-26 13:31:18
Task:Buy Low, Sell High
Sender:Puhi~
Submission time:2018-05-26 13:33:58
Status:READY
Result:WRONG ANSWER

Show test data

Code

#include <iostream>
#include <stdint.h>

using namespace std;

int64_t prices[500000];

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

    for (int i = 0; i < n; i++) {
        cin >> prices[i];
    }
    
    int start = 0;
    int64_t best = 0;
    int best_pos = 0;

    for (int i = 0; i < n; i++) {
        if (prices[i] < prices[start]) {
            start = i;
        }
        
        int64_t p = prices[i] - prices[start];
        if (p > best) {
            best = p;
            best_pos = i;
        }
    }
    
    int bstart = start;
    int bend = best_pos;
    int64_t bbest = best;
    
    //
    start = 0;
    best = 0;
    best_pos = 0;
    
    for (int i = 0; i <= bstart; i++) {
        if (prices[i] < prices[start]) {
            start = i;
        }
        
        int64_t p = prices[i] - prices[start];
        if (p > best) {
            best = p;
            best_pos = i;
        }
    }
    
    int64_t abest = best;
    
    //
    start = bend;
    best = 0;
    best_pos = bend;

    for (int i = bend; i < n; i++) {
        if (prices[i] < prices[start]) {
            start = i;
        }
        
        int64_t p = prices[i] - prices[start];
        if (p > best) {
            best = p;
            best_pos = i;
        }
    }
    
    int64_t ebest = best;
    
    //
    start = bend;
    best = 0;
    best_pos = bend;

    for (int i = bend; i > bstart; i--) {
        if (prices[i] < prices[start]) {
            start = i;
        }
        
        int64_t p = prices[i] - prices[start];
        if (p > best) {
            best = p;
            best_pos = i;
        }
    }
    
    int64_t drop = best;
    
    cout << max(bbest + abest, max(bbest + ebest, bbest + drop)) << endl;
    
}