#include "bits/stdc++.h"
using namespace std;
#define ar array
//~ #define double long double
#define int long long
const long long inf = 1e18;
const int N = 2e5 + 5;
struct node{
int m, b, lx, rx;
double operator * (node& x){
return (x.b - b) * 1. / (m - x.m);
}
int cost(int x){
return m * x + b;
}
};
const long long M = 1e12 + 5;
int t, w;
struct LiChao{
node tree[N * 30] {};
int last;
void init(){
for(int i=0;i<N*30;i++) tree[i] = {w * t, inf, 0, 0};
last = 0;
}
void make(int x){
if(!tree[x].lx) tree[x].lx = ++last;
if(!tree[x].rx) tree[x].rx = ++last;
}
void add(node v, int lx = 0, int rx = M, int x = 0){
if(tree[x].m == v.m){
tree[x].b = min(v.b, tree[x].b);
return;
}
if(lx == rx){
if(tree[x].cost(lx) > v.cost(lx)) swap(tree[x], v);
return;
}
make(x);
double in = tree[x] * v;
int m = (lx + rx) >> 1;
v.lx = tree[x].lx, v.rx = tree[x].rx;
if(m < in){
if(tree[x].cost(m) > v.cost(m)){
swap(tree[x], v);
}
add(v, m + 1, rx, tree[x].rx);
} else {
if(tree[x].cost(m + 1) > v.cost(m + 1)){
swap(tree[x], v);
}
add(v, lx, m, tree[x].lx);
}
}
int get(int i, int lx = 0, int rx = M, int x = 0){
if(lx == rx) return tree[x].cost(i);
int m = (lx + rx) >> 1;
make(x);
if(i <= m) return min(get(i, lx, m, tree[x].lx), tree[x].cost(i));
else return min(get(i, m+1, rx, tree[x].rx), tree[x].cost(i));
}
}tree;
int s[N], d[N], c[N], pref[N], cnt[N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int x, n, m; cin>>x>>n>>m>>w>>t;
tree.init();
for(int i=0;i<n;i++) cin>>s[i];
vector<int> p(m), d_(m), mn(m, x + 1);
int tot = 0;
for(int i=0;i<m;i++){
cin>>d[i]>>c[i];
p[i] = i;
}
tot += (x / t + 1) * w;
sort(p.begin(), p.end(), [&](int i, int j){
return d[i] < d[j];
});
for(int i=0;i<m;i++){
d_[i] = d[p[i]];
cnt[i] = x / t + (d_[i] < x % t);
tot += cnt[i] * w;
if(i) pref[i] += pref[i-1], cnt[i] += cnt[i-1];
pref[i] += c[p[i]];
}
s[n] = x;
for(int i=0;i<=n;i++){
int j = upper_bound(d_.begin(), d_.end(), s[i] % t) - d_.begin();
if(j){ --j;
//~ assert(d_[j] != s[i] % t);
mn[j] = min(mn[j], s[i]);
}
}
//~ cout<<w<<"\n";
//~ for(int i=0;i<m;i++){
//~ cout<<-i * w<<" "<<cnt[i] * w - pref[i]<<"\n";
//~ }
vector<int> dp(m);
int Mx = M / t + 5;
tree.add({w, 0});
for(int i=0;i<m;i++){
if(i) dp[i] = dp[i-1];
if(mn[i] > x){
tree.add({-i * w, dp[i] + cnt[i] * w - pref[i]}, 0, Mx);
continue;
}
int T = mn[i] / t;
dp[i] = min(dp[i], tree.get(T, 0, Mx) - cnt[i] * w + T * i * w + pref[i]);
tree.add({-i * w, dp[i] + cnt[i] * w - pref[i]}, 0, Mx);
}
//~ for(int i=0;i<m;i++) cout<<dp[i]<<" ";
//~ cout<<"\n";
cout<<tot + dp[m-1]<<"\n";
}
/*
0 0 0 0 -23
0 0 0 0 -29
105 3 5 9 10
59
68
71
4 71
6 32
7 29
3 62
2 35
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
188088 KB |
Output is correct |
2 |
Correct |
97 ms |
188168 KB |
Output is correct |
3 |
Correct |
90 ms |
188184 KB |
Output is correct |
4 |
Correct |
91 ms |
188108 KB |
Output is correct |
5 |
Correct |
91 ms |
188172 KB |
Output is correct |
6 |
Correct |
99 ms |
188112 KB |
Output is correct |
7 |
Correct |
90 ms |
188100 KB |
Output is correct |
8 |
Correct |
90 ms |
188172 KB |
Output is correct |
9 |
Correct |
97 ms |
188132 KB |
Output is correct |
10 |
Correct |
89 ms |
188160 KB |
Output is correct |
11 |
Correct |
89 ms |
188204 KB |
Output is correct |
12 |
Correct |
90 ms |
188116 KB |
Output is correct |
13 |
Correct |
96 ms |
188192 KB |
Output is correct |
14 |
Correct |
90 ms |
188188 KB |
Output is correct |
15 |
Correct |
92 ms |
188200 KB |
Output is correct |
16 |
Correct |
90 ms |
188148 KB |
Output is correct |
17 |
Correct |
99 ms |
188144 KB |
Output is correct |
18 |
Correct |
96 ms |
188228 KB |
Output is correct |
19 |
Correct |
91 ms |
188108 KB |
Output is correct |
20 |
Correct |
92 ms |
188144 KB |
Output is correct |
21 |
Correct |
89 ms |
188180 KB |
Output is correct |
22 |
Correct |
97 ms |
188212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
188088 KB |
Output is correct |
2 |
Correct |
97 ms |
188168 KB |
Output is correct |
3 |
Correct |
90 ms |
188184 KB |
Output is correct |
4 |
Correct |
91 ms |
188108 KB |
Output is correct |
5 |
Correct |
91 ms |
188172 KB |
Output is correct |
6 |
Correct |
99 ms |
188112 KB |
Output is correct |
7 |
Correct |
90 ms |
188100 KB |
Output is correct |
8 |
Correct |
90 ms |
188172 KB |
Output is correct |
9 |
Correct |
97 ms |
188132 KB |
Output is correct |
10 |
Correct |
89 ms |
188160 KB |
Output is correct |
11 |
Correct |
89 ms |
188204 KB |
Output is correct |
12 |
Correct |
90 ms |
188116 KB |
Output is correct |
13 |
Correct |
96 ms |
188192 KB |
Output is correct |
14 |
Correct |
90 ms |
188188 KB |
Output is correct |
15 |
Correct |
92 ms |
188200 KB |
Output is correct |
16 |
Correct |
90 ms |
188148 KB |
Output is correct |
17 |
Correct |
99 ms |
188144 KB |
Output is correct |
18 |
Correct |
96 ms |
188228 KB |
Output is correct |
19 |
Correct |
91 ms |
188108 KB |
Output is correct |
20 |
Correct |
92 ms |
188144 KB |
Output is correct |
21 |
Correct |
89 ms |
188180 KB |
Output is correct |
22 |
Correct |
97 ms |
188212 KB |
Output is correct |
23 |
Correct |
96 ms |
188108 KB |
Output is correct |
24 |
Correct |
91 ms |
188124 KB |
Output is correct |
25 |
Correct |
93 ms |
188104 KB |
Output is correct |
26 |
Correct |
91 ms |
188200 KB |
Output is correct |
27 |
Correct |
91 ms |
188204 KB |
Output is correct |
28 |
Correct |
93 ms |
188284 KB |
Output is correct |
29 |
Correct |
90 ms |
188108 KB |
Output is correct |
30 |
Correct |
91 ms |
188220 KB |
Output is correct |
31 |
Correct |
90 ms |
188108 KB |
Output is correct |
32 |
Correct |
90 ms |
188188 KB |
Output is correct |
33 |
Correct |
93 ms |
188156 KB |
Output is correct |
34 |
Correct |
91 ms |
188184 KB |
Output is correct |
35 |
Correct |
90 ms |
188104 KB |
Output is correct |
36 |
Correct |
90 ms |
188176 KB |
Output is correct |
37 |
Correct |
101 ms |
188204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
188088 KB |
Output is correct |
2 |
Correct |
97 ms |
188168 KB |
Output is correct |
3 |
Correct |
90 ms |
188184 KB |
Output is correct |
4 |
Correct |
91 ms |
188108 KB |
Output is correct |
5 |
Correct |
91 ms |
188172 KB |
Output is correct |
6 |
Correct |
99 ms |
188112 KB |
Output is correct |
7 |
Correct |
90 ms |
188100 KB |
Output is correct |
8 |
Correct |
90 ms |
188172 KB |
Output is correct |
9 |
Correct |
97 ms |
188132 KB |
Output is correct |
10 |
Correct |
89 ms |
188160 KB |
Output is correct |
11 |
Correct |
89 ms |
188204 KB |
Output is correct |
12 |
Correct |
90 ms |
188116 KB |
Output is correct |
13 |
Correct |
96 ms |
188192 KB |
Output is correct |
14 |
Correct |
90 ms |
188188 KB |
Output is correct |
15 |
Correct |
92 ms |
188200 KB |
Output is correct |
16 |
Correct |
90 ms |
188148 KB |
Output is correct |
17 |
Correct |
99 ms |
188144 KB |
Output is correct |
18 |
Correct |
96 ms |
188228 KB |
Output is correct |
19 |
Correct |
91 ms |
188108 KB |
Output is correct |
20 |
Correct |
92 ms |
188144 KB |
Output is correct |
21 |
Correct |
89 ms |
188180 KB |
Output is correct |
22 |
Correct |
97 ms |
188212 KB |
Output is correct |
23 |
Correct |
96 ms |
188108 KB |
Output is correct |
24 |
Correct |
91 ms |
188124 KB |
Output is correct |
25 |
Correct |
93 ms |
188104 KB |
Output is correct |
26 |
Correct |
91 ms |
188200 KB |
Output is correct |
27 |
Correct |
91 ms |
188204 KB |
Output is correct |
28 |
Correct |
93 ms |
188284 KB |
Output is correct |
29 |
Correct |
90 ms |
188108 KB |
Output is correct |
30 |
Correct |
91 ms |
188220 KB |
Output is correct |
31 |
Correct |
90 ms |
188108 KB |
Output is correct |
32 |
Correct |
90 ms |
188188 KB |
Output is correct |
33 |
Correct |
93 ms |
188156 KB |
Output is correct |
34 |
Correct |
91 ms |
188184 KB |
Output is correct |
35 |
Correct |
90 ms |
188104 KB |
Output is correct |
36 |
Correct |
90 ms |
188176 KB |
Output is correct |
37 |
Correct |
101 ms |
188204 KB |
Output is correct |
38 |
Correct |
94 ms |
188356 KB |
Output is correct |
39 |
Correct |
92 ms |
188348 KB |
Output is correct |
40 |
Correct |
95 ms |
188360 KB |
Output is correct |
41 |
Correct |
92 ms |
188296 KB |
Output is correct |
42 |
Correct |
92 ms |
188280 KB |
Output is correct |
43 |
Correct |
95 ms |
188364 KB |
Output is correct |
44 |
Correct |
95 ms |
188300 KB |
Output is correct |
45 |
Correct |
95 ms |
188352 KB |
Output is correct |
46 |
Correct |
90 ms |
188316 KB |
Output is correct |
47 |
Correct |
96 ms |
188292 KB |
Output is correct |
48 |
Correct |
97 ms |
188400 KB |
Output is correct |
49 |
Correct |
91 ms |
188304 KB |
Output is correct |
50 |
Correct |
96 ms |
188352 KB |
Output is correct |
51 |
Correct |
92 ms |
188312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
188088 KB |
Output is correct |
2 |
Correct |
97 ms |
188168 KB |
Output is correct |
3 |
Correct |
90 ms |
188184 KB |
Output is correct |
4 |
Correct |
91 ms |
188108 KB |
Output is correct |
5 |
Correct |
91 ms |
188172 KB |
Output is correct |
6 |
Correct |
99 ms |
188112 KB |
Output is correct |
7 |
Correct |
90 ms |
188100 KB |
Output is correct |
8 |
Correct |
90 ms |
188172 KB |
Output is correct |
9 |
Correct |
97 ms |
188132 KB |
Output is correct |
10 |
Correct |
89 ms |
188160 KB |
Output is correct |
11 |
Correct |
89 ms |
188204 KB |
Output is correct |
12 |
Correct |
90 ms |
188116 KB |
Output is correct |
13 |
Correct |
96 ms |
188192 KB |
Output is correct |
14 |
Correct |
90 ms |
188188 KB |
Output is correct |
15 |
Correct |
92 ms |
188200 KB |
Output is correct |
16 |
Correct |
90 ms |
188148 KB |
Output is correct |
17 |
Correct |
99 ms |
188144 KB |
Output is correct |
18 |
Correct |
96 ms |
188228 KB |
Output is correct |
19 |
Correct |
91 ms |
188108 KB |
Output is correct |
20 |
Correct |
92 ms |
188144 KB |
Output is correct |
21 |
Correct |
89 ms |
188180 KB |
Output is correct |
22 |
Correct |
97 ms |
188212 KB |
Output is correct |
23 |
Correct |
96 ms |
188108 KB |
Output is correct |
24 |
Correct |
91 ms |
188124 KB |
Output is correct |
25 |
Correct |
93 ms |
188104 KB |
Output is correct |
26 |
Correct |
91 ms |
188200 KB |
Output is correct |
27 |
Correct |
91 ms |
188204 KB |
Output is correct |
28 |
Correct |
93 ms |
188284 KB |
Output is correct |
29 |
Correct |
90 ms |
188108 KB |
Output is correct |
30 |
Correct |
91 ms |
188220 KB |
Output is correct |
31 |
Correct |
90 ms |
188108 KB |
Output is correct |
32 |
Correct |
90 ms |
188188 KB |
Output is correct |
33 |
Correct |
93 ms |
188156 KB |
Output is correct |
34 |
Correct |
91 ms |
188184 KB |
Output is correct |
35 |
Correct |
90 ms |
188104 KB |
Output is correct |
36 |
Correct |
90 ms |
188176 KB |
Output is correct |
37 |
Correct |
101 ms |
188204 KB |
Output is correct |
38 |
Correct |
94 ms |
188356 KB |
Output is correct |
39 |
Correct |
92 ms |
188348 KB |
Output is correct |
40 |
Correct |
95 ms |
188360 KB |
Output is correct |
41 |
Correct |
92 ms |
188296 KB |
Output is correct |
42 |
Correct |
92 ms |
188280 KB |
Output is correct |
43 |
Correct |
95 ms |
188364 KB |
Output is correct |
44 |
Correct |
95 ms |
188300 KB |
Output is correct |
45 |
Correct |
95 ms |
188352 KB |
Output is correct |
46 |
Correct |
90 ms |
188316 KB |
Output is correct |
47 |
Correct |
96 ms |
188292 KB |
Output is correct |
48 |
Correct |
97 ms |
188400 KB |
Output is correct |
49 |
Correct |
91 ms |
188304 KB |
Output is correct |
50 |
Correct |
96 ms |
188352 KB |
Output is correct |
51 |
Correct |
92 ms |
188312 KB |
Output is correct |
52 |
Correct |
296 ms |
202268 KB |
Output is correct |
53 |
Correct |
306 ms |
208012 KB |
Output is correct |
54 |
Correct |
244 ms |
207072 KB |
Output is correct |
55 |
Correct |
243 ms |
207388 KB |
Output is correct |
56 |
Correct |
242 ms |
207664 KB |
Output is correct |
57 |
Correct |
247 ms |
207384 KB |
Output is correct |
58 |
Correct |
258 ms |
207980 KB |
Output is correct |
59 |
Correct |
236 ms |
207352 KB |
Output is correct |
60 |
Correct |
247 ms |
207356 KB |
Output is correct |
61 |
Correct |
236 ms |
207264 KB |
Output is correct |
62 |
Correct |
245 ms |
207364 KB |
Output is correct |
63 |
Correct |
210 ms |
206272 KB |
Output is correct |
64 |
Correct |
194 ms |
208104 KB |
Output is correct |
65 |
Correct |
285 ms |
207924 KB |
Output is correct |
66 |
Correct |
297 ms |
208076 KB |
Output is correct |
67 |
Correct |
308 ms |
208032 KB |
Output is correct |
68 |
Correct |
300 ms |
207400 KB |
Output is correct |
69 |
Correct |
279 ms |
207272 KB |
Output is correct |
70 |
Correct |
274 ms |
206276 KB |
Output is correct |
71 |
Correct |
279 ms |
206300 KB |
Output is correct |
72 |
Correct |
271 ms |
205484 KB |
Output is correct |
73 |
Correct |
274 ms |
205504 KB |
Output is correct |
74 |
Correct |
265 ms |
204488 KB |
Output is correct |
75 |
Correct |
260 ms |
204492 KB |
Output is correct |
76 |
Correct |
264 ms |
203724 KB |
Output is correct |
77 |
Correct |
237 ms |
203692 KB |
Output is correct |
78 |
Correct |
254 ms |
202828 KB |
Output is correct |
79 |
Correct |
261 ms |
202800 KB |
Output is correct |
80 |
Correct |
263 ms |
202188 KB |
Output is correct |