#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 11;
int a[N], dp[N];
int m[N], c[N], l, r;
int floor_div(int a, int b){
return a / b - (a % b != 0 && a < 0);
}
int interX(int m1, int c1, int m2, int c2){
return floor_div(c2 - c1, m1 - m2);
}
void add(int _m, int _c){
while(r - l >= 2 && interX(m[r - 2], c[r - 2], _m, _c) <= interX(m[r - 2], c[r - 2], m[r - 1], c[r - 1])) r--;
m[r] = _m, c[r] = _c; r++;
}
int qry(int x){
while(r - l >= 2 && m[l] * x + c[l] >= m[l + 1] * x + c[l + 1]) l++;
return m[l] * x + c[l];
}
int32_t main(){
cin.tie(0); cout.tie(0)->sync_with_stdio(false);
int n; cin >> n;
for(int i = 0; i < n; i++) cin >> a[i];
for(int i = 1; i < n; i++) a[i] = max(a[i - 1], a[i]);
memset(dp, 0x3f, sizeof(dp)); dp[0] = 0;
for(int i = 0; i < n; i++){
add(-i, dp[i]);
dp[i + 1] = a[i] * n + qry(a[i]);
}
cout << dp[n] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12644 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 |
12632 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 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 |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 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 |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
12888 KB |
Output is correct |
10 |
Correct |
2 ms |
12632 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12776 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 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 |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12632 KB |
Output is correct |
9 |
Correct |
2 ms |
12888 KB |
Output is correct |
10 |
Correct |
2 ms |
12632 KB |
Output is correct |
11 |
Correct |
2 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12776 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12784 KB |
Output is correct |
16 |
Correct |
83 ms |
19024 KB |
Output is correct |
17 |
Correct |
94 ms |
25136 KB |
Output is correct |
18 |
Correct |
77 ms |
23628 KB |
Output is correct |
19 |
Correct |
91 ms |
24916 KB |
Output is correct |
20 |
Correct |
90 ms |
24916 KB |
Output is correct |
21 |
Correct |
89 ms |
25024 KB |
Output is correct |
22 |
Correct |
88 ms |
24640 KB |
Output is correct |
23 |
Correct |
99 ms |
25704 KB |
Output is correct |
24 |
Correct |
92 ms |
24904 KB |
Output is correct |
25 |
Correct |
98 ms |
25288 KB |
Output is correct |
26 |
Correct |
98 ms |
25808 KB |
Output is correct |
27 |
Correct |
87 ms |
24264 KB |
Output is correct |
28 |
Correct |
99 ms |
25020 KB |
Output is correct |
29 |
Correct |
102 ms |
25352 KB |
Output is correct |
30 |
Correct |
95 ms |
25412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
19144 KB |
Output is correct |
2 |
Correct |
102 ms |
19292 KB |
Output is correct |
3 |
Correct |
96 ms |
19024 KB |
Output is correct |
4 |
Correct |
94 ms |
19136 KB |
Output is correct |
5 |
Correct |
94 ms |
19148 KB |
Output is correct |
6 |
Correct |
103 ms |
19284 KB |
Output is correct |
7 |
Correct |
96 ms |
19256 KB |
Output is correct |
8 |
Correct |
95 ms |
19024 KB |
Output is correct |
9 |
Correct |
95 ms |
19024 KB |
Output is correct |
10 |
Correct |
95 ms |
19396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12644 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 |
12632 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 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 |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 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 |
12636 KB |
Output is correct |
21 |
Correct |
2 ms |
12636 KB |
Output is correct |
22 |
Correct |
2 ms |
12632 KB |
Output is correct |
23 |
Correct |
2 ms |
12888 KB |
Output is correct |
24 |
Correct |
2 ms |
12632 KB |
Output is correct |
25 |
Correct |
2 ms |
12636 KB |
Output is correct |
26 |
Correct |
2 ms |
12776 KB |
Output is correct |
27 |
Correct |
2 ms |
12636 KB |
Output is correct |
28 |
Correct |
3 ms |
12636 KB |
Output is correct |
29 |
Correct |
2 ms |
12784 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 |
12636 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 |
12632 KB |
Output is correct |
37 |
Correct |
2 ms |
12636 KB |
Output is correct |
38 |
Correct |
2 ms |
12636 KB |
Output is correct |
39 |
Correct |
2 ms |
12636 KB |
Output is correct |
40 |
Correct |
2 ms |
12636 KB |
Output is correct |
41 |
Correct |
2 ms |
12636 KB |
Output is correct |
42 |
Correct |
2 ms |
12636 KB |
Output is correct |
43 |
Correct |
2 ms |
12636 KB |
Output is correct |
44 |
Correct |
2 ms |
12636 KB |
Output is correct |
45 |
Correct |
3 ms |
12772 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 |
12832 KB |
Output is correct |
49 |
Correct |
2 ms |
12636 KB |
Output is correct |
50 |
Correct |
2 ms |
12776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12644 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 |
12632 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 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 |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 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 |
12636 KB |
Output is correct |
21 |
Correct |
2 ms |
12636 KB |
Output is correct |
22 |
Correct |
2 ms |
12632 KB |
Output is correct |
23 |
Correct |
2 ms |
12888 KB |
Output is correct |
24 |
Correct |
2 ms |
12632 KB |
Output is correct |
25 |
Correct |
2 ms |
12636 KB |
Output is correct |
26 |
Correct |
2 ms |
12776 KB |
Output is correct |
27 |
Correct |
2 ms |
12636 KB |
Output is correct |
28 |
Correct |
3 ms |
12636 KB |
Output is correct |
29 |
Correct |
2 ms |
12784 KB |
Output is correct |
30 |
Correct |
83 ms |
19024 KB |
Output is correct |
31 |
Correct |
94 ms |
25136 KB |
Output is correct |
32 |
Correct |
77 ms |
23628 KB |
Output is correct |
33 |
Correct |
91 ms |
24916 KB |
Output is correct |
34 |
Correct |
90 ms |
24916 KB |
Output is correct |
35 |
Correct |
89 ms |
25024 KB |
Output is correct |
36 |
Correct |
88 ms |
24640 KB |
Output is correct |
37 |
Correct |
99 ms |
25704 KB |
Output is correct |
38 |
Correct |
92 ms |
24904 KB |
Output is correct |
39 |
Correct |
98 ms |
25288 KB |
Output is correct |
40 |
Correct |
98 ms |
25808 KB |
Output is correct |
41 |
Correct |
87 ms |
24264 KB |
Output is correct |
42 |
Correct |
99 ms |
25020 KB |
Output is correct |
43 |
Correct |
102 ms |
25352 KB |
Output is correct |
44 |
Correct |
95 ms |
25412 KB |
Output is correct |
45 |
Correct |
96 ms |
19144 KB |
Output is correct |
46 |
Correct |
102 ms |
19292 KB |
Output is correct |
47 |
Correct |
96 ms |
19024 KB |
Output is correct |
48 |
Correct |
94 ms |
19136 KB |
Output is correct |
49 |
Correct |
94 ms |
19148 KB |
Output is correct |
50 |
Correct |
103 ms |
19284 KB |
Output is correct |
51 |
Correct |
96 ms |
19256 KB |
Output is correct |
52 |
Correct |
95 ms |
19024 KB |
Output is correct |
53 |
Correct |
95 ms |
19024 KB |
Output is correct |
54 |
Correct |
95 ms |
19396 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 |
12636 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 |
12632 KB |
Output is correct |
62 |
Correct |
2 ms |
12636 KB |
Output is correct |
63 |
Correct |
2 ms |
12636 KB |
Output is correct |
64 |
Correct |
2 ms |
12636 KB |
Output is correct |
65 |
Correct |
2 ms |
12636 KB |
Output is correct |
66 |
Correct |
2 ms |
12636 KB |
Output is correct |
67 |
Correct |
2 ms |
12636 KB |
Output is correct |
68 |
Correct |
2 ms |
12636 KB |
Output is correct |
69 |
Correct |
2 ms |
12636 KB |
Output is correct |
70 |
Correct |
3 ms |
12772 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 |
12832 KB |
Output is correct |
74 |
Correct |
2 ms |
12636 KB |
Output is correct |
75 |
Correct |
2 ms |
12776 KB |
Output is correct |
76 |
Correct |
89 ms |
25172 KB |
Output is correct |
77 |
Correct |
89 ms |
25172 KB |
Output is correct |
78 |
Correct |
102 ms |
24656 KB |
Output is correct |
79 |
Correct |
88 ms |
25016 KB |
Output is correct |
80 |
Correct |
93 ms |
25428 KB |
Output is correct |
81 |
Correct |
84 ms |
24660 KB |
Output is correct |
82 |
Correct |
84 ms |
24756 KB |
Output is correct |
83 |
Correct |
89 ms |
25172 KB |
Output is correct |
84 |
Correct |
93 ms |
24904 KB |
Output is correct |
85 |
Correct |
94 ms |
25540 KB |
Output is correct |
86 |
Correct |
83 ms |
24556 KB |
Output is correct |
87 |
Correct |
85 ms |
24404 KB |
Output is correct |
88 |
Correct |
88 ms |
24912 KB |
Output is correct |
89 |
Correct |
87 ms |
24912 KB |
Output is correct |
90 |
Correct |
88 ms |
24656 KB |
Output is correct |
91 |
Correct |
87 ms |
24400 KB |
Output is correct |
92 |
Correct |
100 ms |
25804 KB |
Output is correct |
93 |
Correct |
90 ms |
25028 KB |
Output is correct |
94 |
Correct |
85 ms |
24132 KB |
Output is correct |
95 |
Correct |
93 ms |
25564 KB |
Output is correct |
96 |
Correct |
84 ms |
24400 KB |
Output is correct |
97 |
Correct |
92 ms |
24660 KB |
Output is correct |
98 |
Correct |
86 ms |
24920 KB |
Output is correct |
99 |
Correct |
86 ms |
24660 KB |
Output is correct |
100 |
Correct |
90 ms |
24776 KB |
Output is correct |