#include <bits/stdc++.h>
#define int long long
using namespace std;
int nums[1000001], dp[1000001];
deque<pair<int,int> > dq;
int f(pair<int,int> line, int x){
return line.first*x + line.second;
}
int query(int x){
while(dq.size()>1){ //to prevent seg fault when accessing dq[1]
if(f(dq[0],x)<f(dq[1],x)) //the next line is better
dq.pop_front(); //remove useless line
else break;
}
return f(dq[0],x);
}
double intersect(int m1, int c1, int m2, int c2){
return (double)(c2-c1)/(m1-m2);
}
double intersect(pair<int,int> p1, pair<int,int> p2){
return intersect(p1.first,p1.second,p2.first,p2.second);
}
void insert(int m,int c) {//insert line y=mx+c
pair<int,int> line = make_pair(m,c);
while (dq.size()>1) { //to prevent seg fault
int s = dq.size();
if (intersect(dq[s-1], line) <= intersect(dq[s-2], line))
dq.pop_back(); //removes useless line
else break;
}
dq.push_back(line);
}
main(){
int n; cin >> n;
for (int i = 1; i <= n; i++){
dp[i] = 1e16;
cin >> nums[i];
nums[i] = max(nums[i], nums[i - 1]);
}
for (int i = 1; i <= n; i++){
insert(i, -dp[i - 1]);
dp[i] = -query(nums[i]) + nums[i] * n + nums[i];
/*
for (int j = 1; j <= i; j++){
dp[i] = min(dp[i], - nums[i] * j + dp[j - 1] + nums[i] * n + nums[i]));
}
*/
}
cout << dp[n] << '\n';
}
Compilation message
Discharging.cpp:33:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
33 | main(){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
0 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
2 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
2 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
304 ms |
16100 KB |
Output is correct |
17 |
Correct |
330 ms |
21936 KB |
Output is correct |
18 |
Correct |
280 ms |
20524 KB |
Output is correct |
19 |
Correct |
327 ms |
21860 KB |
Output is correct |
20 |
Correct |
326 ms |
21732 KB |
Output is correct |
21 |
Correct |
332 ms |
21732 KB |
Output is correct |
22 |
Correct |
315 ms |
21364 KB |
Output is correct |
23 |
Correct |
350 ms |
22372 KB |
Output is correct |
24 |
Correct |
332 ms |
21984 KB |
Output is correct |
25 |
Correct |
345 ms |
22452 KB |
Output is correct |
26 |
Correct |
363 ms |
22756 KB |
Output is correct |
27 |
Correct |
302 ms |
21092 KB |
Output is correct |
28 |
Correct |
332 ms |
21980 KB |
Output is correct |
29 |
Correct |
339 ms |
22116 KB |
Output is correct |
30 |
Correct |
350 ms |
22372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
435 ms |
15908 KB |
Output is correct |
2 |
Correct |
442 ms |
25700 KB |
Output is correct |
3 |
Correct |
442 ms |
25700 KB |
Output is correct |
4 |
Correct |
446 ms |
25700 KB |
Output is correct |
5 |
Correct |
440 ms |
25692 KB |
Output is correct |
6 |
Correct |
443 ms |
25688 KB |
Output is correct |
7 |
Correct |
443 ms |
25828 KB |
Output is correct |
8 |
Correct |
441 ms |
25700 KB |
Output is correct |
9 |
Correct |
444 ms |
25700 KB |
Output is correct |
10 |
Correct |
442 ms |
25700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
0 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
1 ms |
364 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
1 ms |
364 KB |
Output is correct |
42 |
Correct |
1 ms |
364 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
1 ms |
364 KB |
Output is correct |
50 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
0 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
304 ms |
16100 KB |
Output is correct |
31 |
Correct |
330 ms |
21936 KB |
Output is correct |
32 |
Correct |
280 ms |
20524 KB |
Output is correct |
33 |
Correct |
327 ms |
21860 KB |
Output is correct |
34 |
Correct |
326 ms |
21732 KB |
Output is correct |
35 |
Correct |
332 ms |
21732 KB |
Output is correct |
36 |
Correct |
315 ms |
21364 KB |
Output is correct |
37 |
Correct |
350 ms |
22372 KB |
Output is correct |
38 |
Correct |
332 ms |
21984 KB |
Output is correct |
39 |
Correct |
345 ms |
22452 KB |
Output is correct |
40 |
Correct |
363 ms |
22756 KB |
Output is correct |
41 |
Correct |
302 ms |
21092 KB |
Output is correct |
42 |
Correct |
332 ms |
21980 KB |
Output is correct |
43 |
Correct |
339 ms |
22116 KB |
Output is correct |
44 |
Correct |
350 ms |
22372 KB |
Output is correct |
45 |
Correct |
435 ms |
15908 KB |
Output is correct |
46 |
Correct |
442 ms |
25700 KB |
Output is correct |
47 |
Correct |
442 ms |
25700 KB |
Output is correct |
48 |
Correct |
446 ms |
25700 KB |
Output is correct |
49 |
Correct |
440 ms |
25692 KB |
Output is correct |
50 |
Correct |
443 ms |
25688 KB |
Output is correct |
51 |
Correct |
443 ms |
25828 KB |
Output is correct |
52 |
Correct |
441 ms |
25700 KB |
Output is correct |
53 |
Correct |
444 ms |
25700 KB |
Output is correct |
54 |
Correct |
442 ms |
25700 KB |
Output is correct |
55 |
Correct |
1 ms |
364 KB |
Output is correct |
56 |
Correct |
1 ms |
364 KB |
Output is correct |
57 |
Correct |
1 ms |
364 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
1 ms |
364 KB |
Output is correct |
62 |
Correct |
1 ms |
364 KB |
Output is correct |
63 |
Correct |
1 ms |
364 KB |
Output is correct |
64 |
Correct |
1 ms |
364 KB |
Output is correct |
65 |
Correct |
1 ms |
364 KB |
Output is correct |
66 |
Correct |
1 ms |
364 KB |
Output is correct |
67 |
Correct |
1 ms |
364 KB |
Output is correct |
68 |
Correct |
1 ms |
364 KB |
Output is correct |
69 |
Correct |
1 ms |
364 KB |
Output is correct |
70 |
Correct |
1 ms |
364 KB |
Output is correct |
71 |
Correct |
1 ms |
364 KB |
Output is correct |
72 |
Correct |
1 ms |
364 KB |
Output is correct |
73 |
Correct |
1 ms |
364 KB |
Output is correct |
74 |
Correct |
1 ms |
364 KB |
Output is correct |
75 |
Correct |
1 ms |
364 KB |
Output is correct |
76 |
Correct |
334 ms |
22116 KB |
Output is correct |
77 |
Correct |
324 ms |
21988 KB |
Output is correct |
78 |
Correct |
308 ms |
21476 KB |
Output is correct |
79 |
Correct |
316 ms |
21732 KB |
Output is correct |
80 |
Correct |
339 ms |
22372 KB |
Output is correct |
81 |
Correct |
311 ms |
21472 KB |
Output is correct |
82 |
Correct |
311 ms |
21476 KB |
Output is correct |
83 |
Correct |
328 ms |
21988 KB |
Output is correct |
84 |
Correct |
323 ms |
21892 KB |
Output is correct |
85 |
Correct |
332 ms |
21988 KB |
Output is correct |
86 |
Correct |
310 ms |
21604 KB |
Output is correct |
87 |
Correct |
311 ms |
21360 KB |
Output is correct |
88 |
Correct |
321 ms |
22008 KB |
Output is correct |
89 |
Correct |
310 ms |
21476 KB |
Output is correct |
90 |
Correct |
315 ms |
21520 KB |
Output is correct |
91 |
Correct |
317 ms |
21476 KB |
Output is correct |
92 |
Correct |
396 ms |
22652 KB |
Output is correct |
93 |
Correct |
326 ms |
21860 KB |
Output is correct |
94 |
Correct |
292 ms |
21092 KB |
Output is correct |
95 |
Correct |
337 ms |
22500 KB |
Output is correct |
96 |
Correct |
303 ms |
21348 KB |
Output is correct |
97 |
Correct |
317 ms |
21860 KB |
Output is correct |
98 |
Correct |
323 ms |
21860 KB |
Output is correct |
99 |
Correct |
312 ms |
21604 KB |
Output is correct |
100 |
Correct |
313 ms |
21604 KB |
Output is correct |