#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 1e6+11;
int arr[MAXN], dp[MAXN], A[MAXN], B[MAXN];
int L,R;
int teil(int a, int b){
return a/b-(a % b != 0 && a < 0);
}
int steigung(int a1, int b1, int a2, int b2){
return teil(b2-b1,a1-a2);
}
void upd(int i, int x){
while(R-L >= 2 && steigung(A[R-2],B[R-2],i,x) <= steigung(A[R-2],B[R-2],A[R-1],B[R-1])) R--;
A[R] = i; B[R] = x; R++;
}
int query(int x){
while(R-L >= 2 && A[L]*x+B[L] >= A[L+1]*x+B[L+1]) L++;
return A[L]*x+B[L];
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N; cin >> N;
for(int i = 0;i<N;++i) cin >> arr[i];
for(int i = 1;i<N;++i) arr[i] = max(arr[i],arr[i-1]);
memset(dp,0x3f,sizeof(dp)); dp[0] = 0;
for(int i = 0;i<N;++i){
upd(-i, dp[i]);
dp[i+1] = arr[i]*N+query(arr[i]);
}
cout << dp[N] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12760 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12752 KB |
Output is correct |
6 |
Correct |
2 ms |
12760 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
12632 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12772 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12632 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12792 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
2 ms |
12772 KB |
Output is correct |
7 |
Correct |
2 ms |
12632 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12632 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12792 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12760 KB |
Output is correct |
16 |
Correct |
82 ms |
24404 KB |
Output is correct |
17 |
Correct |
90 ms |
25124 KB |
Output is correct |
18 |
Correct |
75 ms |
23484 KB |
Output is correct |
19 |
Correct |
88 ms |
24916 KB |
Output is correct |
20 |
Correct |
86 ms |
24904 KB |
Output is correct |
21 |
Correct |
90 ms |
25112 KB |
Output is correct |
22 |
Correct |
84 ms |
24636 KB |
Output is correct |
23 |
Correct |
97 ms |
25684 KB |
Output is correct |
24 |
Correct |
90 ms |
25028 KB |
Output is correct |
25 |
Correct |
93 ms |
25424 KB |
Output is correct |
26 |
Correct |
95 ms |
25688 KB |
Output is correct |
27 |
Correct |
88 ms |
24324 KB |
Output is correct |
28 |
Correct |
98 ms |
25432 KB |
Output is correct |
29 |
Correct |
96 ms |
25144 KB |
Output is correct |
30 |
Correct |
92 ms |
25536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
94 ms |
28760 KB |
Output is correct |
2 |
Correct |
95 ms |
28728 KB |
Output is correct |
3 |
Correct |
95 ms |
29008 KB |
Output is correct |
4 |
Correct |
94 ms |
28736 KB |
Output is correct |
5 |
Correct |
94 ms |
28856 KB |
Output is correct |
6 |
Correct |
95 ms |
28856 KB |
Output is correct |
7 |
Correct |
94 ms |
28752 KB |
Output is correct |
8 |
Correct |
98 ms |
28724 KB |
Output is correct |
9 |
Correct |
95 ms |
28756 KB |
Output is correct |
10 |
Correct |
94 ms |
28752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12760 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12752 KB |
Output is correct |
6 |
Correct |
2 ms |
12760 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
12632 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12632 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
12636 KB |
Output is correct |
20 |
Correct |
2 ms |
12772 KB |
Output is correct |
21 |
Correct |
2 ms |
12632 KB |
Output is correct |
22 |
Correct |
2 ms |
12636 KB |
Output is correct |
23 |
Correct |
2 ms |
12632 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
2 ms |
12636 KB |
Output is correct |
26 |
Correct |
2 ms |
12792 KB |
Output is correct |
27 |
Correct |
2 ms |
12636 KB |
Output is correct |
28 |
Correct |
2 ms |
12636 KB |
Output is correct |
29 |
Correct |
2 ms |
12760 KB |
Output is correct |
30 |
Correct |
2 ms |
12636 KB |
Output is correct |
31 |
Correct |
2 ms |
12636 KB |
Output is correct |
32 |
Correct |
2 ms |
12632 KB |
Output is correct |
33 |
Correct |
2 ms |
12636 KB |
Output is correct |
34 |
Correct |
2 ms |
12636 KB |
Output is correct |
35 |
Correct |
2 ms |
12636 KB |
Output is correct |
36 |
Correct |
2 ms |
12636 KB |
Output is correct |
37 |
Correct |
2 ms |
12760 KB |
Output is correct |
38 |
Correct |
2 ms |
12636 KB |
Output is correct |
39 |
Correct |
2 ms |
12784 KB |
Output is correct |
40 |
Correct |
2 ms |
12636 KB |
Output is correct |
41 |
Correct |
2 ms |
12784 KB |
Output is correct |
42 |
Correct |
2 ms |
12636 KB |
Output is correct |
43 |
Correct |
2 ms |
12636 KB |
Output is correct |
44 |
Correct |
3 ms |
12636 KB |
Output is correct |
45 |
Correct |
2 ms |
12888 KB |
Output is correct |
46 |
Correct |
2 ms |
12636 KB |
Output is correct |
47 |
Correct |
2 ms |
12636 KB |
Output is correct |
48 |
Correct |
2 ms |
12636 KB |
Output is correct |
49 |
Correct |
2 ms |
12636 KB |
Output is correct |
50 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12760 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12752 KB |
Output is correct |
6 |
Correct |
2 ms |
12760 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12632 KB |
Output is correct |
13 |
Correct |
2 ms |
12632 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12632 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 ms |
12636 KB |
Output is correct |
20 |
Correct |
2 ms |
12772 KB |
Output is correct |
21 |
Correct |
2 ms |
12632 KB |
Output is correct |
22 |
Correct |
2 ms |
12636 KB |
Output is correct |
23 |
Correct |
2 ms |
12632 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
2 ms |
12636 KB |
Output is correct |
26 |
Correct |
2 ms |
12792 KB |
Output is correct |
27 |
Correct |
2 ms |
12636 KB |
Output is correct |
28 |
Correct |
2 ms |
12636 KB |
Output is correct |
29 |
Correct |
2 ms |
12760 KB |
Output is correct |
30 |
Correct |
82 ms |
24404 KB |
Output is correct |
31 |
Correct |
90 ms |
25124 KB |
Output is correct |
32 |
Correct |
75 ms |
23484 KB |
Output is correct |
33 |
Correct |
88 ms |
24916 KB |
Output is correct |
34 |
Correct |
86 ms |
24904 KB |
Output is correct |
35 |
Correct |
90 ms |
25112 KB |
Output is correct |
36 |
Correct |
84 ms |
24636 KB |
Output is correct |
37 |
Correct |
97 ms |
25684 KB |
Output is correct |
38 |
Correct |
90 ms |
25028 KB |
Output is correct |
39 |
Correct |
93 ms |
25424 KB |
Output is correct |
40 |
Correct |
95 ms |
25688 KB |
Output is correct |
41 |
Correct |
88 ms |
24324 KB |
Output is correct |
42 |
Correct |
98 ms |
25432 KB |
Output is correct |
43 |
Correct |
96 ms |
25144 KB |
Output is correct |
44 |
Correct |
92 ms |
25536 KB |
Output is correct |
45 |
Correct |
94 ms |
28760 KB |
Output is correct |
46 |
Correct |
95 ms |
28728 KB |
Output is correct |
47 |
Correct |
95 ms |
29008 KB |
Output is correct |
48 |
Correct |
94 ms |
28736 KB |
Output is correct |
49 |
Correct |
94 ms |
28856 KB |
Output is correct |
50 |
Correct |
95 ms |
28856 KB |
Output is correct |
51 |
Correct |
94 ms |
28752 KB |
Output is correct |
52 |
Correct |
98 ms |
28724 KB |
Output is correct |
53 |
Correct |
95 ms |
28756 KB |
Output is correct |
54 |
Correct |
94 ms |
28752 KB |
Output is correct |
55 |
Correct |
2 ms |
12636 KB |
Output is correct |
56 |
Correct |
2 ms |
12636 KB |
Output is correct |
57 |
Correct |
2 ms |
12632 KB |
Output is correct |
58 |
Correct |
2 ms |
12636 KB |
Output is correct |
59 |
Correct |
2 ms |
12636 KB |
Output is correct |
60 |
Correct |
2 ms |
12636 KB |
Output is correct |
61 |
Correct |
2 ms |
12636 KB |
Output is correct |
62 |
Correct |
2 ms |
12760 KB |
Output is correct |
63 |
Correct |
2 ms |
12636 KB |
Output is correct |
64 |
Correct |
2 ms |
12784 KB |
Output is correct |
65 |
Correct |
2 ms |
12636 KB |
Output is correct |
66 |
Correct |
2 ms |
12784 KB |
Output is correct |
67 |
Correct |
2 ms |
12636 KB |
Output is correct |
68 |
Correct |
2 ms |
12636 KB |
Output is correct |
69 |
Correct |
3 ms |
12636 KB |
Output is correct |
70 |
Correct |
2 ms |
12888 KB |
Output is correct |
71 |
Correct |
2 ms |
12636 KB |
Output is correct |
72 |
Correct |
2 ms |
12636 KB |
Output is correct |
73 |
Correct |
2 ms |
12636 KB |
Output is correct |
74 |
Correct |
2 ms |
12636 KB |
Output is correct |
75 |
Correct |
2 ms |
12636 KB |
Output is correct |
76 |
Correct |
88 ms |
25036 KB |
Output is correct |
77 |
Correct |
86 ms |
25152 KB |
Output is correct |
78 |
Correct |
86 ms |
24656 KB |
Output is correct |
79 |
Correct |
84 ms |
24916 KB |
Output is correct |
80 |
Correct |
93 ms |
25668 KB |
Output is correct |
81 |
Correct |
82 ms |
24804 KB |
Output is correct |
82 |
Correct |
83 ms |
24656 KB |
Output is correct |
83 |
Correct |
88 ms |
25148 KB |
Output is correct |
84 |
Correct |
87 ms |
24916 KB |
Output is correct |
85 |
Correct |
87 ms |
25172 KB |
Output is correct |
86 |
Correct |
83 ms |
24716 KB |
Output is correct |
87 |
Correct |
84 ms |
24384 KB |
Output is correct |
88 |
Correct |
87 ms |
24892 KB |
Output is correct |
89 |
Correct |
86 ms |
24764 KB |
Output is correct |
90 |
Correct |
84 ms |
24660 KB |
Output is correct |
91 |
Correct |
80 ms |
24656 KB |
Output is correct |
92 |
Correct |
94 ms |
25684 KB |
Output is correct |
93 |
Correct |
90 ms |
25392 KB |
Output is correct |
94 |
Correct |
81 ms |
24116 KB |
Output is correct |
95 |
Correct |
88 ms |
25432 KB |
Output is correct |
96 |
Correct |
83 ms |
24404 KB |
Output is correct |
97 |
Correct |
83 ms |
24656 KB |
Output is correct |
98 |
Correct |
84 ms |
24912 KB |
Output is correct |
99 |
Correct |
83 ms |
24660 KB |
Output is correct |
100 |
Correct |
84 ms |
24760 KB |
Output is correct |