Submission
Status:
[PPPPPPPPPPPPP]
Subtask/Task Score:
{100/100}
Score: 100
User: Jokul
Problemset: ซื้อขายหุ้นซีเค
Language: cpp
Time: 0.010 second
Submitted On: 2026-03-18 19:38:38
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<long long> p(n);
for (int i = 0; i < n; i++) cin >> p[i];
long long profit = 0;
int i = 0;
while (i < n - 1) {
// find valley (buy)
while (i < n - 1 && p[i] >= p[i + 1]) i++;
int buy = p[i];
// find peak (sell)
while (i < n - 1 && p[i] <= p[i + 1]) i++;
int sell = p[i];
profit += (sell - buy);
}
cout << profit << '\n';
}