// Source: https://usaco.guide/general/io
#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
using namespace std;
const int N = 1e6 + 5;
ll n, a[N], dp[N];
ll inf = 1e15;
struct Line {
ll k;
ll b;
ll val(ll x) {
return k *x + b;
}
pair < ll , ll > intr(Line yo) {
ll one = yo.b - b;
ll two = k - yo.k;
if (two < 0) one *= -1, two *= -1;
return {one, two};
}
};
vector < Line > dq;
inline void upd(ll k, ll b) {
Line lin = {k, b};
while (dq.size() > 1) {
Line lina = dq.end()[-1];
Line linb = dq.end()[-2];
pair < ll , ll > A = lin.intr(lina);
pair < ll , ll > B = lina.intr(linb);
if (A.f / A.s <= B.f / B.s) {
dq.pop_back();
}
else break;
}
dq.push_back(lin);
}
ll get(ll x) {
int l = 1, r = dq.size() - 1, mid, res = 0;
while (l <= r) {
mid = ((l + r) >> 1);
if (dq[mid - 1].val(x) >= dq[mid].val(x)) {
l = mid + 1, res = mid;
}
else {
r = mid - 1;
}
}
return dq[res].val(x);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (1 < i && a[i - 1] > a[i]) {
a[i] = a[i - 1];
}
}
dq.push_back({0, 0});
for (int i = 1; i <= n; ++i) {
/*
dp[i] = inf;
for (int j = i - 1; j >= 0; --j) {
dp[i] = min(dp[i], dp[j] - j * a[i] + n * a[i]);
} */
dp[i] = get(a[i]) + n * a[i];
upd(-i, dp[i]);
}
cout << dp[n] << "\n";
}
/*
dp[......]
dp[1..j]
.... j + 1 ... i = max is a[i]
dp[i] = dp[j] + ( n - j ) * a[i]
dp[i] = dp[j] - j * a[i] + n * a[i];
Y = K * X + B
X = a[i]
K = -j;
B = dp[j];
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
97 ms |
15904 KB |
Output is correct |
17 |
Correct |
104 ms |
15904 KB |
Output is correct |
18 |
Correct |
93 ms |
15960 KB |
Output is correct |
19 |
Correct |
112 ms |
15948 KB |
Output is correct |
20 |
Correct |
103 ms |
15856 KB |
Output is correct |
21 |
Correct |
100 ms |
15892 KB |
Output is correct |
22 |
Correct |
98 ms |
15856 KB |
Output is correct |
23 |
Correct |
117 ms |
15912 KB |
Output is correct |
24 |
Correct |
104 ms |
15948 KB |
Output is correct |
25 |
Correct |
109 ms |
15944 KB |
Output is correct |
26 |
Correct |
115 ms |
16080 KB |
Output is correct |
27 |
Correct |
101 ms |
15948 KB |
Output is correct |
28 |
Correct |
101 ms |
15956 KB |
Output is correct |
29 |
Correct |
109 ms |
15916 KB |
Output is correct |
30 |
Correct |
118 ms |
15928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
15896 KB |
Output is correct |
2 |
Correct |
114 ms |
15912 KB |
Output is correct |
3 |
Correct |
112 ms |
15892 KB |
Output is correct |
4 |
Correct |
113 ms |
15948 KB |
Output is correct |
5 |
Correct |
132 ms |
15992 KB |
Output is correct |
6 |
Correct |
103 ms |
15944 KB |
Output is correct |
7 |
Correct |
105 ms |
15876 KB |
Output is correct |
8 |
Correct |
103 ms |
15948 KB |
Output is correct |
9 |
Correct |
115 ms |
15948 KB |
Output is correct |
10 |
Correct |
109 ms |
15900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
0 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 |
340 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 |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 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 |
340 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 |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
97 ms |
15904 KB |
Output is correct |
31 |
Correct |
104 ms |
15904 KB |
Output is correct |
32 |
Correct |
93 ms |
15960 KB |
Output is correct |
33 |
Correct |
112 ms |
15948 KB |
Output is correct |
34 |
Correct |
103 ms |
15856 KB |
Output is correct |
35 |
Correct |
100 ms |
15892 KB |
Output is correct |
36 |
Correct |
98 ms |
15856 KB |
Output is correct |
37 |
Correct |
117 ms |
15912 KB |
Output is correct |
38 |
Correct |
104 ms |
15948 KB |
Output is correct |
39 |
Correct |
109 ms |
15944 KB |
Output is correct |
40 |
Correct |
115 ms |
16080 KB |
Output is correct |
41 |
Correct |
101 ms |
15948 KB |
Output is correct |
42 |
Correct |
101 ms |
15956 KB |
Output is correct |
43 |
Correct |
109 ms |
15916 KB |
Output is correct |
44 |
Correct |
118 ms |
15928 KB |
Output is correct |
45 |
Correct |
130 ms |
15896 KB |
Output is correct |
46 |
Correct |
114 ms |
15912 KB |
Output is correct |
47 |
Correct |
112 ms |
15892 KB |
Output is correct |
48 |
Correct |
113 ms |
15948 KB |
Output is correct |
49 |
Correct |
132 ms |
15992 KB |
Output is correct |
50 |
Correct |
103 ms |
15944 KB |
Output is correct |
51 |
Correct |
105 ms |
15876 KB |
Output is correct |
52 |
Correct |
103 ms |
15948 KB |
Output is correct |
53 |
Correct |
115 ms |
15948 KB |
Output is correct |
54 |
Correct |
109 ms |
15900 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
0 ms |
340 KB |
Output is correct |
57 |
Correct |
0 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 |
340 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 |
340 KB |
Output is correct |
67 |
Correct |
1 ms |
340 KB |
Output is correct |
68 |
Correct |
1 ms |
340 KB |
Output is correct |
69 |
Correct |
0 ms |
340 KB |
Output is correct |
70 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
74 |
Correct |
1 ms |
340 KB |
Output is correct |
75 |
Correct |
1 ms |
340 KB |
Output is correct |
76 |
Correct |
101 ms |
15948 KB |
Output is correct |
77 |
Correct |
108 ms |
15908 KB |
Output is correct |
78 |
Correct |
105 ms |
15928 KB |
Output is correct |
79 |
Correct |
118 ms |
15840 KB |
Output is correct |
80 |
Correct |
114 ms |
15828 KB |
Output is correct |
81 |
Correct |
102 ms |
15896 KB |
Output is correct |
82 |
Correct |
117 ms |
15856 KB |
Output is correct |
83 |
Correct |
119 ms |
15920 KB |
Output is correct |
84 |
Correct |
107 ms |
15948 KB |
Output is correct |
85 |
Correct |
107 ms |
15848 KB |
Output is correct |
86 |
Correct |
107 ms |
15864 KB |
Output is correct |
87 |
Correct |
108 ms |
15872 KB |
Output is correct |
88 |
Correct |
108 ms |
15928 KB |
Output is correct |
89 |
Correct |
109 ms |
15928 KB |
Output is correct |
90 |
Correct |
104 ms |
15916 KB |
Output is correct |
91 |
Correct |
106 ms |
15856 KB |
Output is correct |
92 |
Correct |
122 ms |
15888 KB |
Output is correct |
93 |
Correct |
118 ms |
15932 KB |
Output is correct |
94 |
Correct |
99 ms |
15916 KB |
Output is correct |
95 |
Correct |
129 ms |
15928 KB |
Output is correct |
96 |
Correct |
119 ms |
15860 KB |
Output is correct |
97 |
Correct |
105 ms |
15960 KB |
Output is correct |
98 |
Correct |
107 ms |
15912 KB |
Output is correct |
99 |
Correct |
105 ms |
15932 KB |
Output is correct |
100 |
Correct |
121 ms |
15904 KB |
Output is correct |