Submission

Status:

[PPPPPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: mantaggez

Problemset: ซื้อขายหุ้นซีเค

Language: cpp

Time: 0.009 second

Submitted On: 2026-03-20 22:35:31

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll nx = 1e5+5;
const ll INF = 1e18;

ll n;
ll p[nx];

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin >> n;
    fill(p, p + nx, INF);
    for(ll i=1;i<=n;i++) cin >> p[i];

    ll ans = 0, sum = 0, buy = p[1];
    for(ll i=2;i<=n;i++) {
        if((p[i] > p[i + 1] || i == n) && p[i] > buy) {
            // cout << "Sell : " << i << '\n';
            ans += p[i] - buy;
            buy = INF;
        }
        else buy = min(buy, p[i]);
    } 

    cout << ans << '\n';

    return 0;
}