#include <bits/stdc++.h>
using namespace std;
#define int long long
deque<pair<int, int> > dq;
int f(pair<int, int> x, int y){
return x.first*y + x.second;
}
long double intersect(int a, int b, int c, int d){
return (long double)(d-b)/(a-c);
}
void ins(int m , int c){
while(dq.size() > 1){
int s = dq.size();
if(intersect(dq.back().first, dq.back().second,m,c) <= intersect(dq[s-2].first, dq[s-2].second, m,c))dq.pop_back();
else break;
}
dq.push_back(make_pair(m,c));
}
int query(int x){
while(dq.size() > 1){
if(f(dq[0], x) <= f(dq[1], x))dq.pop_front();
// <= for max dp, >= for min dp
else break;
}
return f(dq[0], x);
}
int dp[1000005], A[1000005];
int Pmax[1000005];
main(){
ios::sync_with_stdio(0);cin.tie(0);
int n;cin >> n;
for(int i=1;i<=n;i++){
cin >> A[i];Pmax[i] = max(Pmax[i-1], A[i]);
}
dq.push_back(make_pair(0, 0));
for(int i=1;i<=n;i++){
int x = Pmax[i];
dp[i] = x*n - query(x);
ins(i, -dp[i]);
}
cout << dp[n];
}
Compilation message
Discharging.cpp:30:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
30 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
360 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
360 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
137 ms |
28756 KB |
Output is correct |
17 |
Correct |
124 ms |
29592 KB |
Output is correct |
18 |
Correct |
115 ms |
28160 KB |
Output is correct |
19 |
Correct |
130 ms |
29520 KB |
Output is correct |
20 |
Correct |
120 ms |
29416 KB |
Output is correct |
21 |
Correct |
118 ms |
29492 KB |
Output is correct |
22 |
Correct |
128 ms |
29048 KB |
Output is correct |
23 |
Correct |
116 ms |
30148 KB |
Output is correct |
24 |
Correct |
126 ms |
29712 KB |
Output is correct |
25 |
Correct |
123 ms |
29984 KB |
Output is correct |
26 |
Correct |
120 ms |
30328 KB |
Output is correct |
27 |
Correct |
115 ms |
28828 KB |
Output is correct |
28 |
Correct |
128 ms |
29652 KB |
Output is correct |
29 |
Correct |
146 ms |
29832 KB |
Output is correct |
30 |
Correct |
122 ms |
30140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
33372 KB |
Output is correct |
2 |
Correct |
138 ms |
33420 KB |
Output is correct |
3 |
Correct |
134 ms |
33440 KB |
Output is correct |
4 |
Correct |
125 ms |
33396 KB |
Output is correct |
5 |
Correct |
143 ms |
33412 KB |
Output is correct |
6 |
Correct |
160 ms |
33352 KB |
Output is correct |
7 |
Correct |
136 ms |
33464 KB |
Output is correct |
8 |
Correct |
129 ms |
33392 KB |
Output is correct |
9 |
Correct |
152 ms |
33340 KB |
Output is correct |
10 |
Correct |
131 ms |
33452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
360 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
360 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
336 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
360 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
360 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
137 ms |
28756 KB |
Output is correct |
31 |
Correct |
124 ms |
29592 KB |
Output is correct |
32 |
Correct |
115 ms |
28160 KB |
Output is correct |
33 |
Correct |
130 ms |
29520 KB |
Output is correct |
34 |
Correct |
120 ms |
29416 KB |
Output is correct |
35 |
Correct |
118 ms |
29492 KB |
Output is correct |
36 |
Correct |
128 ms |
29048 KB |
Output is correct |
37 |
Correct |
116 ms |
30148 KB |
Output is correct |
38 |
Correct |
126 ms |
29712 KB |
Output is correct |
39 |
Correct |
123 ms |
29984 KB |
Output is correct |
40 |
Correct |
120 ms |
30328 KB |
Output is correct |
41 |
Correct |
115 ms |
28828 KB |
Output is correct |
42 |
Correct |
128 ms |
29652 KB |
Output is correct |
43 |
Correct |
146 ms |
29832 KB |
Output is correct |
44 |
Correct |
122 ms |
30140 KB |
Output is correct |
45 |
Correct |
132 ms |
33372 KB |
Output is correct |
46 |
Correct |
138 ms |
33420 KB |
Output is correct |
47 |
Correct |
134 ms |
33440 KB |
Output is correct |
48 |
Correct |
125 ms |
33396 KB |
Output is correct |
49 |
Correct |
143 ms |
33412 KB |
Output is correct |
50 |
Correct |
160 ms |
33352 KB |
Output is correct |
51 |
Correct |
136 ms |
33464 KB |
Output is correct |
52 |
Correct |
129 ms |
33392 KB |
Output is correct |
53 |
Correct |
152 ms |
33340 KB |
Output is correct |
54 |
Correct |
131 ms |
33452 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
1 ms |
340 KB |
Output is correct |
59 |
Correct |
1 ms |
340 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
1 ms |
340 KB |
Output is correct |
62 |
Correct |
1 ms |
340 KB |
Output is correct |
63 |
Correct |
1 ms |
340 KB |
Output is correct |
64 |
Correct |
1 ms |
340 KB |
Output is correct |
65 |
Correct |
1 ms |
340 KB |
Output is correct |
66 |
Correct |
1 ms |
336 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
1 ms |
340 KB |
Output is correct |
69 |
Correct |
1 ms |
340 KB |
Output is correct |
70 |
Correct |
1 ms |
336 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
1 ms |
340 KB |
Output is correct |
73 |
Correct |
1 ms |
336 KB |
Output is correct |
74 |
Correct |
1 ms |
340 KB |
Output is correct |
75 |
Correct |
1 ms |
340 KB |
Output is correct |
76 |
Correct |
122 ms |
29752 KB |
Output is correct |
77 |
Correct |
125 ms |
29740 KB |
Output is correct |
78 |
Correct |
130 ms |
29188 KB |
Output is correct |
79 |
Correct |
116 ms |
29372 KB |
Output is correct |
80 |
Correct |
113 ms |
30056 KB |
Output is correct |
81 |
Correct |
103 ms |
29336 KB |
Output is correct |
82 |
Correct |
128 ms |
29248 KB |
Output is correct |
83 |
Correct |
121 ms |
29644 KB |
Output is correct |
84 |
Correct |
110 ms |
29544 KB |
Output is correct |
85 |
Correct |
109 ms |
29840 KB |
Output is correct |
86 |
Correct |
108 ms |
29216 KB |
Output is correct |
87 |
Correct |
115 ms |
29076 KB |
Output is correct |
88 |
Correct |
112 ms |
29388 KB |
Output is correct |
89 |
Correct |
118 ms |
29260 KB |
Output is correct |
90 |
Correct |
129 ms |
29208 KB |
Output is correct |
91 |
Correct |
106 ms |
28920 KB |
Output is correct |
92 |
Correct |
125 ms |
30244 KB |
Output is correct |
93 |
Correct |
120 ms |
29484 KB |
Output is correct |
94 |
Correct |
100 ms |
28696 KB |
Output is correct |
95 |
Correct |
116 ms |
30052 KB |
Output is correct |
96 |
Correct |
122 ms |
29040 KB |
Output is correct |
97 |
Correct |
108 ms |
29360 KB |
Output is correct |
98 |
Correct |
106 ms |
29544 KB |
Output is correct |
99 |
Correct |
124 ms |
29132 KB |
Output is correct |
100 |
Correct |
113 ms |
29260 KB |
Output is correct |