#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 55;
ll dp[MAXN], T[MAXN];
ll f (array<ll, 2> line, ll x) {
return line[0] * x + line[1];
}
long double inters (array<ll, 2> line1, array<ll, 2> line2) {
return (line2[1] - line1[1]) / (line1[0] - line2[0]);
}
struct Hull {
deque<array<ll, 2>> dq;
ll query (ll x) {
while ((int)dq.size() > 1 && f(dq[0], x) > f(dq[1], x)) dq.pop_front();
return f(dq[0], x);
}
void insert (array<ll, 2> line) {
while (dq.size() > 1 && inters(line, dq.back()) <= inters(line, dq[dq.size() - 2])) dq.pop_back();
dq.push_back(line);
}
};
int32_t main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
for (int i = 1;i <= N;i ++) {
cin >> T[i];
}
Hull hl;
ll mx = 1, ind = 1;
for (int i = 1;i <= N;i ++) {
if (T[i] >= T[mx]) {
mx = i;
}
while (ind <= mx){
hl.insert({-1LL * ind, dp[ind - 1]});
ind ++;
}
if (i > 1) {
dp[i] = min(T[mx] * N, hl.query(T[mx]) + T[mx] * (N + 1));
} else {
dp[i] = T[1] * N;
}
}
cout << dp[N] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
0 ms |
352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
356 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
516 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
356 KB |
Output is correct |
6 |
Correct |
1 ms |
476 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
356 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
356 KB |
Output is correct |
2 |
Correct |
1 ms |
352 KB |
Output is correct |
3 |
Correct |
0 ms |
516 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
356 KB |
Output is correct |
6 |
Correct |
1 ms |
476 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
356 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
81 ms |
21128 KB |
Output is correct |
17 |
Correct |
77 ms |
21844 KB |
Output is correct |
18 |
Correct |
73 ms |
20348 KB |
Output is correct |
19 |
Correct |
86 ms |
21720 KB |
Output is correct |
20 |
Correct |
78 ms |
21520 KB |
Output is correct |
21 |
Correct |
86 ms |
21688 KB |
Output is correct |
22 |
Correct |
73 ms |
21328 KB |
Output is correct |
23 |
Correct |
77 ms |
22424 KB |
Output is correct |
24 |
Correct |
71 ms |
21744 KB |
Output is correct |
25 |
Correct |
74 ms |
22352 KB |
Output is correct |
26 |
Correct |
75 ms |
22724 KB |
Output is correct |
27 |
Correct |
73 ms |
21076 KB |
Output is correct |
28 |
Correct |
69 ms |
21936 KB |
Output is correct |
29 |
Correct |
72 ms |
22096 KB |
Output is correct |
30 |
Correct |
74 ms |
22356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
23952 KB |
Output is correct |
2 |
Correct |
81 ms |
23588 KB |
Output is correct |
3 |
Correct |
86 ms |
23632 KB |
Output is correct |
4 |
Correct |
87 ms |
23580 KB |
Output is correct |
5 |
Correct |
84 ms |
23532 KB |
Output is correct |
6 |
Correct |
80 ms |
23740 KB |
Output is correct |
7 |
Correct |
80 ms |
23616 KB |
Output is correct |
8 |
Correct |
80 ms |
23636 KB |
Output is correct |
9 |
Correct |
80 ms |
23740 KB |
Output is correct |
10 |
Correct |
82 ms |
23636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
0 ms |
352 KB |
Output is correct |
15 |
Correct |
1 ms |
356 KB |
Output is correct |
16 |
Correct |
1 ms |
352 KB |
Output is correct |
17 |
Correct |
0 ms |
516 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
356 KB |
Output is correct |
20 |
Correct |
1 ms |
476 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
356 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
352 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
348 KB |
Output is correct |
50 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
352 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
352 KB |
Output is correct |
14 |
Correct |
0 ms |
352 KB |
Output is correct |
15 |
Correct |
1 ms |
356 KB |
Output is correct |
16 |
Correct |
1 ms |
352 KB |
Output is correct |
17 |
Correct |
0 ms |
516 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
356 KB |
Output is correct |
20 |
Correct |
1 ms |
476 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
356 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
352 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
81 ms |
21128 KB |
Output is correct |
31 |
Correct |
77 ms |
21844 KB |
Output is correct |
32 |
Correct |
73 ms |
20348 KB |
Output is correct |
33 |
Correct |
86 ms |
21720 KB |
Output is correct |
34 |
Correct |
78 ms |
21520 KB |
Output is correct |
35 |
Correct |
86 ms |
21688 KB |
Output is correct |
36 |
Correct |
73 ms |
21328 KB |
Output is correct |
37 |
Correct |
77 ms |
22424 KB |
Output is correct |
38 |
Correct |
71 ms |
21744 KB |
Output is correct |
39 |
Correct |
74 ms |
22352 KB |
Output is correct |
40 |
Correct |
75 ms |
22724 KB |
Output is correct |
41 |
Correct |
73 ms |
21076 KB |
Output is correct |
42 |
Correct |
69 ms |
21936 KB |
Output is correct |
43 |
Correct |
72 ms |
22096 KB |
Output is correct |
44 |
Correct |
74 ms |
22356 KB |
Output is correct |
45 |
Correct |
83 ms |
23952 KB |
Output is correct |
46 |
Correct |
81 ms |
23588 KB |
Output is correct |
47 |
Correct |
86 ms |
23632 KB |
Output is correct |
48 |
Correct |
87 ms |
23580 KB |
Output is correct |
49 |
Correct |
84 ms |
23532 KB |
Output is correct |
50 |
Correct |
80 ms |
23740 KB |
Output is correct |
51 |
Correct |
80 ms |
23616 KB |
Output is correct |
52 |
Correct |
80 ms |
23636 KB |
Output is correct |
53 |
Correct |
80 ms |
23740 KB |
Output is correct |
54 |
Correct |
82 ms |
23636 KB |
Output is correct |
55 |
Correct |
0 ms |
604 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
0 ms |
348 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
0 ms |
348 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
0 ms |
348 KB |
Output is correct |
65 |
Correct |
1 ms |
344 KB |
Output is correct |
66 |
Correct |
1 ms |
348 KB |
Output is correct |
67 |
Correct |
1 ms |
348 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
348 KB |
Output is correct |
71 |
Correct |
1 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
348 KB |
Output is correct |
74 |
Correct |
1 ms |
348 KB |
Output is correct |
75 |
Correct |
1 ms |
348 KB |
Output is correct |
76 |
Correct |
75 ms |
22096 KB |
Output is correct |
77 |
Correct |
80 ms |
22096 KB |
Output is correct |
78 |
Correct |
74 ms |
21332 KB |
Output is correct |
79 |
Correct |
70 ms |
21584 KB |
Output is correct |
80 |
Correct |
77 ms |
22424 KB |
Output is correct |
81 |
Correct |
71 ms |
21584 KB |
Output is correct |
82 |
Correct |
69 ms |
21444 KB |
Output is correct |
83 |
Correct |
75 ms |
21844 KB |
Output is correct |
84 |
Correct |
79 ms |
21840 KB |
Output is correct |
85 |
Correct |
75 ms |
22100 KB |
Output is correct |
86 |
Correct |
85 ms |
21360 KB |
Output is correct |
87 |
Correct |
69 ms |
21328 KB |
Output is correct |
88 |
Correct |
66 ms |
21704 KB |
Output is correct |
89 |
Correct |
73 ms |
21584 KB |
Output is correct |
90 |
Correct |
67 ms |
21492 KB |
Output is correct |
91 |
Correct |
64 ms |
21288 KB |
Output is correct |
92 |
Correct |
78 ms |
22624 KB |
Output is correct |
93 |
Correct |
73 ms |
21740 KB |
Output is correct |
94 |
Correct |
66 ms |
20828 KB |
Output is correct |
95 |
Correct |
71 ms |
22256 KB |
Output is correct |
96 |
Correct |
73 ms |
21328 KB |
Output is correct |
97 |
Correct |
73 ms |
21588 KB |
Output is correct |
98 |
Correct |
75 ms |
21848 KB |
Output is correct |
99 |
Correct |
70 ms |
21332 KB |
Output is correct |
100 |
Correct |
73 ms |
21528 KB |
Output is correct |