-
Notifications
You must be signed in to change notification settings - Fork 0
/
G.cpp
48 lines (37 loc) · 949 Bytes
/
G.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<int> B(N), J(N);
for (int i = 0; i < N; i++) {
cin >> B[i];
}
for (int i = 0; i < N; i++) {
cin >> J[i];
}
vector<int> maxSellAfter(N);
maxSellAfter[N-1] = J[N-1];
for (int i = N-2; i >= 0; i--) {
maxSellAfter[i] = max(J[i], maxSellAfter[i+1]);
}
int maxProfit = 0;
int minBuy = INT_MAX;
for (int i = 0; i < N - 2; i++) {
minBuy = min(minBuy, B[i]);
if (i + 2 < N && maxSellAfter[i+2] > minBuy) {
maxProfit = max(maxProfit, maxSellAfter[i+2] - minBuy);
}
}
if (maxProfit > 0) {
cout << maxProfit << '\n';
} else {
cout << "tidak mungkin\n";
}
return 0;
}