#include <bits/extc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#define int long long
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define debug(x) do{auto y = x; cout << #x << " = " << y << endl;}while(0)
// #define endl "\n"
#define unordered_map __gnu_pbds::gp_hash_table
using pii = pair<int, int>;
const int inf = 1ll << 60;
const int MOD = 1e9 + 7;
int n;
vector<int> t;
int eval(pii f, int x) {
if(x >= f.f) return inf;
return (n - x) * t[f.f - 1] + f.s;
}
struct lichao {
int tl, tr;
pii f;
lichao *lc, *rc;
void insert(pii f2) {
int tm = (tl + tr) / 2;
if(eval(f2, tm) < eval(f, tm)) swap(f, f2);
if(tl + 1 == tr) return;
if(eval(f2, tl) <= eval(f, tl)) {
if(!lc) {
lc = new lichao{tl, tm, f2};
} else {
lc->insert(f2);
}
} else if(eval(f2, tr - 1) <= eval(f, tr - 1)) {
if(!rc) {
rc = new lichao{tm, tr, f2};
} else {
rc->insert(f2);
}
}
}
int query(int x) {
int tm = (tl + tr) / 2;
int res = eval(f, x);
if(x < tm && lc) return min(res, lc->query(x));
if(x >= tm && rc) return min(res, rc->query(x));
return res;
}
};
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
t.resize(n);
for(int i = 0; i < n; i++) {
cin >> t[i];
}
for(int i = 1; i < n; i++) {
t[i] = max(t[i], t[i - 1]);
}
vector<int> dp(n);
lichao lct{0, n, {n, 0}};
for(int i = n - 1; i >= 0; i--) {
dp[i] = lct.query(i);
lct.insert({i, dp[i]});
}
cout << dp[0] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 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 |
0 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 |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 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 |
0 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 |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
124 ms |
16004 KB |
Output is correct |
17 |
Correct |
127 ms |
15956 KB |
Output is correct |
18 |
Correct |
116 ms |
15948 KB |
Output is correct |
19 |
Correct |
126 ms |
16092 KB |
Output is correct |
20 |
Correct |
129 ms |
15944 KB |
Output is correct |
21 |
Correct |
140 ms |
16072 KB |
Output is correct |
22 |
Correct |
125 ms |
15960 KB |
Output is correct |
23 |
Correct |
122 ms |
16004 KB |
Output is correct |
24 |
Correct |
132 ms |
16080 KB |
Output is correct |
25 |
Correct |
135 ms |
15952 KB |
Output is correct |
26 |
Correct |
139 ms |
16252 KB |
Output is correct |
27 |
Correct |
130 ms |
15960 KB |
Output is correct |
28 |
Correct |
126 ms |
15948 KB |
Output is correct |
29 |
Correct |
126 ms |
16072 KB |
Output is correct |
30 |
Correct |
128 ms |
15924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
16100 KB |
Output is correct |
2 |
Correct |
104 ms |
15948 KB |
Output is correct |
3 |
Correct |
99 ms |
15972 KB |
Output is correct |
4 |
Correct |
102 ms |
15948 KB |
Output is correct |
5 |
Correct |
104 ms |
15964 KB |
Output is correct |
6 |
Correct |
102 ms |
15948 KB |
Output is correct |
7 |
Correct |
110 ms |
15952 KB |
Output is correct |
8 |
Correct |
97 ms |
15948 KB |
Output is correct |
9 |
Correct |
99 ms |
15956 KB |
Output is correct |
10 |
Correct |
95 ms |
15964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
1 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 |
0 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 |
0 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 |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
0 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 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 |
1 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 |
0 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 |
0 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 |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 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 |
124 ms |
16004 KB |
Output is correct |
31 |
Correct |
127 ms |
15956 KB |
Output is correct |
32 |
Correct |
116 ms |
15948 KB |
Output is correct |
33 |
Correct |
126 ms |
16092 KB |
Output is correct |
34 |
Correct |
129 ms |
15944 KB |
Output is correct |
35 |
Correct |
140 ms |
16072 KB |
Output is correct |
36 |
Correct |
125 ms |
15960 KB |
Output is correct |
37 |
Correct |
122 ms |
16004 KB |
Output is correct |
38 |
Correct |
132 ms |
16080 KB |
Output is correct |
39 |
Correct |
135 ms |
15952 KB |
Output is correct |
40 |
Correct |
139 ms |
16252 KB |
Output is correct |
41 |
Correct |
130 ms |
15960 KB |
Output is correct |
42 |
Correct |
126 ms |
15948 KB |
Output is correct |
43 |
Correct |
126 ms |
16072 KB |
Output is correct |
44 |
Correct |
128 ms |
15924 KB |
Output is correct |
45 |
Correct |
101 ms |
16100 KB |
Output is correct |
46 |
Correct |
104 ms |
15948 KB |
Output is correct |
47 |
Correct |
99 ms |
15972 KB |
Output is correct |
48 |
Correct |
102 ms |
15948 KB |
Output is correct |
49 |
Correct |
104 ms |
15964 KB |
Output is correct |
50 |
Correct |
102 ms |
15948 KB |
Output is correct |
51 |
Correct |
110 ms |
15952 KB |
Output is correct |
52 |
Correct |
97 ms |
15948 KB |
Output is correct |
53 |
Correct |
99 ms |
15956 KB |
Output is correct |
54 |
Correct |
95 ms |
15964 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
70 |
Correct |
0 ms |
340 KB |
Output is correct |
71 |
Correct |
1 ms |
340 KB |
Output is correct |
72 |
Correct |
0 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 |
115 ms |
15948 KB |
Output is correct |
77 |
Correct |
115 ms |
22092 KB |
Output is correct |
78 |
Correct |
126 ms |
21432 KB |
Output is correct |
79 |
Correct |
105 ms |
21572 KB |
Output is correct |
80 |
Correct |
115 ms |
22216 KB |
Output is correct |
81 |
Correct |
129 ms |
21584 KB |
Output is correct |
82 |
Correct |
121 ms |
21448 KB |
Output is correct |
83 |
Correct |
113 ms |
21948 KB |
Output is correct |
84 |
Correct |
115 ms |
21708 KB |
Output is correct |
85 |
Correct |
121 ms |
22052 KB |
Output is correct |
86 |
Correct |
113 ms |
21456 KB |
Output is correct |
87 |
Correct |
123 ms |
21456 KB |
Output is correct |
88 |
Correct |
108 ms |
21712 KB |
Output is correct |
89 |
Correct |
128 ms |
21448 KB |
Output is correct |
90 |
Correct |
109 ms |
21444 KB |
Output is correct |
91 |
Correct |
109 ms |
21232 KB |
Output is correct |
92 |
Correct |
135 ms |
22548 KB |
Output is correct |
93 |
Correct |
129 ms |
21964 KB |
Output is correct |
94 |
Correct |
108 ms |
20916 KB |
Output is correct |
95 |
Correct |
127 ms |
22352 KB |
Output is correct |
96 |
Correct |
104 ms |
21232 KB |
Output is correct |
97 |
Correct |
113 ms |
21588 KB |
Output is correct |
98 |
Correct |
112 ms |
21704 KB |
Output is correct |
99 |
Correct |
123 ms |
21436 KB |
Output is correct |
100 |
Correct |
114 ms |
21572 KB |
Output is correct |