#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
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
188108 KB |
Output is correct |
2 |
Correct |
94 ms |
188212 KB |
Output is correct |
3 |
Correct |
93 ms |
188088 KB |
Output is correct |
4 |
Correct |
90 ms |
188124 KB |
Output is correct |
5 |
Correct |
89 ms |
188200 KB |
Output is correct |
6 |
Correct |
91 ms |
188108 KB |
Output is correct |
7 |
Correct |
99 ms |
188180 KB |
Output is correct |
8 |
Correct |
92 ms |
188184 KB |
Output is correct |
9 |
Correct |
91 ms |
188188 KB |
Output is correct |
10 |
Correct |
91 ms |
188124 KB |
Output is correct |
11 |
Correct |
91 ms |
188184 KB |
Output is correct |
12 |
Correct |
91 ms |
188172 KB |
Output is correct |
13 |
Correct |
99 ms |
188124 KB |
Output is correct |
14 |
Correct |
101 ms |
188148 KB |
Output is correct |
15 |
Correct |
91 ms |
188156 KB |
Output is correct |
16 |
Correct |
91 ms |
188112 KB |
Output is correct |
17 |
Correct |
90 ms |
188108 KB |
Output is correct |
18 |
Correct |
93 ms |
188140 KB |
Output is correct |
19 |
Correct |
91 ms |
188164 KB |
Output is correct |
20 |
Correct |
103 ms |
188176 KB |
Output is correct |
21 |
Correct |
91 ms |
188156 KB |
Output is correct |
22 |
Correct |
92 ms |
188176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
188108 KB |
Output is correct |
2 |
Correct |
94 ms |
188212 KB |
Output is correct |
3 |
Correct |
93 ms |
188088 KB |
Output is correct |
4 |
Correct |
90 ms |
188124 KB |
Output is correct |
5 |
Correct |
89 ms |
188200 KB |
Output is correct |
6 |
Correct |
91 ms |
188108 KB |
Output is correct |
7 |
Correct |
99 ms |
188180 KB |
Output is correct |
8 |
Correct |
92 ms |
188184 KB |
Output is correct |
9 |
Correct |
91 ms |
188188 KB |
Output is correct |
10 |
Correct |
91 ms |
188124 KB |
Output is correct |
11 |
Correct |
91 ms |
188184 KB |
Output is correct |
12 |
Correct |
91 ms |
188172 KB |
Output is correct |
13 |
Correct |
99 ms |
188124 KB |
Output is correct |
14 |
Correct |
101 ms |
188148 KB |
Output is correct |
15 |
Correct |
91 ms |
188156 KB |
Output is correct |
16 |
Correct |
91 ms |
188112 KB |
Output is correct |
17 |
Correct |
90 ms |
188108 KB |
Output is correct |
18 |
Correct |
93 ms |
188140 KB |
Output is correct |
19 |
Correct |
91 ms |
188164 KB |
Output is correct |
20 |
Correct |
103 ms |
188176 KB |
Output is correct |
21 |
Correct |
91 ms |
188156 KB |
Output is correct |
22 |
Correct |
92 ms |
188176 KB |
Output is correct |
23 |
Correct |
93 ms |
188196 KB |
Output is correct |
24 |
Correct |
92 ms |
188216 KB |
Output is correct |
25 |
Correct |
101 ms |
188108 KB |
Output is correct |
26 |
Correct |
93 ms |
188140 KB |
Output is correct |
27 |
Correct |
89 ms |
188204 KB |
Output is correct |
28 |
Correct |
94 ms |
188108 KB |
Output is correct |
29 |
Correct |
92 ms |
188204 KB |
Output is correct |
30 |
Correct |
92 ms |
188144 KB |
Output is correct |
31 |
Correct |
90 ms |
188208 KB |
Output is correct |
32 |
Correct |
90 ms |
188136 KB |
Output is correct |
33 |
Correct |
89 ms |
188212 KB |
Output is correct |
34 |
Correct |
93 ms |
188228 KB |
Output is correct |
35 |
Correct |
92 ms |
188180 KB |
Output is correct |
36 |
Correct |
101 ms |
188112 KB |
Output is correct |
37 |
Correct |
91 ms |
188196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
188108 KB |
Output is correct |
2 |
Correct |
94 ms |
188212 KB |
Output is correct |
3 |
Correct |
93 ms |
188088 KB |
Output is correct |
4 |
Correct |
90 ms |
188124 KB |
Output is correct |
5 |
Correct |
89 ms |
188200 KB |
Output is correct |
6 |
Correct |
91 ms |
188108 KB |
Output is correct |
7 |
Correct |
99 ms |
188180 KB |
Output is correct |
8 |
Correct |
92 ms |
188184 KB |
Output is correct |
9 |
Correct |
91 ms |
188188 KB |
Output is correct |
10 |
Correct |
91 ms |
188124 KB |
Output is correct |
11 |
Correct |
91 ms |
188184 KB |
Output is correct |
12 |
Correct |
91 ms |
188172 KB |
Output is correct |
13 |
Correct |
99 ms |
188124 KB |
Output is correct |
14 |
Correct |
101 ms |
188148 KB |
Output is correct |
15 |
Correct |
91 ms |
188156 KB |
Output is correct |
16 |
Correct |
91 ms |
188112 KB |
Output is correct |
17 |
Correct |
90 ms |
188108 KB |
Output is correct |
18 |
Correct |
93 ms |
188140 KB |
Output is correct |
19 |
Correct |
91 ms |
188164 KB |
Output is correct |
20 |
Correct |
103 ms |
188176 KB |
Output is correct |
21 |
Correct |
91 ms |
188156 KB |
Output is correct |
22 |
Correct |
92 ms |
188176 KB |
Output is correct |
23 |
Correct |
93 ms |
188196 KB |
Output is correct |
24 |
Correct |
92 ms |
188216 KB |
Output is correct |
25 |
Correct |
101 ms |
188108 KB |
Output is correct |
26 |
Correct |
93 ms |
188140 KB |
Output is correct |
27 |
Correct |
89 ms |
188204 KB |
Output is correct |
28 |
Correct |
94 ms |
188108 KB |
Output is correct |
29 |
Correct |
92 ms |
188204 KB |
Output is correct |
30 |
Correct |
92 ms |
188144 KB |
Output is correct |
31 |
Correct |
90 ms |
188208 KB |
Output is correct |
32 |
Correct |
90 ms |
188136 KB |
Output is correct |
33 |
Correct |
89 ms |
188212 KB |
Output is correct |
34 |
Correct |
93 ms |
188228 KB |
Output is correct |
35 |
Correct |
92 ms |
188180 KB |
Output is correct |
36 |
Correct |
101 ms |
188112 KB |
Output is correct |
37 |
Correct |
91 ms |
188196 KB |
Output is correct |
38 |
Correct |
95 ms |
188280 KB |
Output is correct |
39 |
Correct |
93 ms |
188272 KB |
Output is correct |
40 |
Correct |
96 ms |
188328 KB |
Output is correct |
41 |
Correct |
99 ms |
188340 KB |
Output is correct |
42 |
Correct |
93 ms |
188268 KB |
Output is correct |
43 |
Correct |
98 ms |
188324 KB |
Output is correct |
44 |
Correct |
92 ms |
188312 KB |
Output is correct |
45 |
Correct |
92 ms |
188376 KB |
Output is correct |
46 |
Correct |
93 ms |
188340 KB |
Output is correct |
47 |
Correct |
94 ms |
188396 KB |
Output is correct |
48 |
Correct |
93 ms |
188352 KB |
Output is correct |
49 |
Correct |
104 ms |
188364 KB |
Output is correct |
50 |
Correct |
94 ms |
188320 KB |
Output is correct |
51 |
Correct |
93 ms |
188304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
188108 KB |
Output is correct |
2 |
Correct |
94 ms |
188212 KB |
Output is correct |
3 |
Correct |
93 ms |
188088 KB |
Output is correct |
4 |
Correct |
90 ms |
188124 KB |
Output is correct |
5 |
Correct |
89 ms |
188200 KB |
Output is correct |
6 |
Correct |
91 ms |
188108 KB |
Output is correct |
7 |
Correct |
99 ms |
188180 KB |
Output is correct |
8 |
Correct |
92 ms |
188184 KB |
Output is correct |
9 |
Correct |
91 ms |
188188 KB |
Output is correct |
10 |
Correct |
91 ms |
188124 KB |
Output is correct |
11 |
Correct |
91 ms |
188184 KB |
Output is correct |
12 |
Correct |
91 ms |
188172 KB |
Output is correct |
13 |
Correct |
99 ms |
188124 KB |
Output is correct |
14 |
Correct |
101 ms |
188148 KB |
Output is correct |
15 |
Correct |
91 ms |
188156 KB |
Output is correct |
16 |
Correct |
91 ms |
188112 KB |
Output is correct |
17 |
Correct |
90 ms |
188108 KB |
Output is correct |
18 |
Correct |
93 ms |
188140 KB |
Output is correct |
19 |
Correct |
91 ms |
188164 KB |
Output is correct |
20 |
Correct |
103 ms |
188176 KB |
Output is correct |
21 |
Correct |
91 ms |
188156 KB |
Output is correct |
22 |
Correct |
92 ms |
188176 KB |
Output is correct |
23 |
Correct |
93 ms |
188196 KB |
Output is correct |
24 |
Correct |
92 ms |
188216 KB |
Output is correct |
25 |
Correct |
101 ms |
188108 KB |
Output is correct |
26 |
Correct |
93 ms |
188140 KB |
Output is correct |
27 |
Correct |
89 ms |
188204 KB |
Output is correct |
28 |
Correct |
94 ms |
188108 KB |
Output is correct |
29 |
Correct |
92 ms |
188204 KB |
Output is correct |
30 |
Correct |
92 ms |
188144 KB |
Output is correct |
31 |
Correct |
90 ms |
188208 KB |
Output is correct |
32 |
Correct |
90 ms |
188136 KB |
Output is correct |
33 |
Correct |
89 ms |
188212 KB |
Output is correct |
34 |
Correct |
93 ms |
188228 KB |
Output is correct |
35 |
Correct |
92 ms |
188180 KB |
Output is correct |
36 |
Correct |
101 ms |
188112 KB |
Output is correct |
37 |
Correct |
91 ms |
188196 KB |
Output is correct |
38 |
Correct |
95 ms |
188280 KB |
Output is correct |
39 |
Correct |
93 ms |
188272 KB |
Output is correct |
40 |
Correct |
96 ms |
188328 KB |
Output is correct |
41 |
Correct |
99 ms |
188340 KB |
Output is correct |
42 |
Correct |
93 ms |
188268 KB |
Output is correct |
43 |
Correct |
98 ms |
188324 KB |
Output is correct |
44 |
Correct |
92 ms |
188312 KB |
Output is correct |
45 |
Correct |
92 ms |
188376 KB |
Output is correct |
46 |
Correct |
93 ms |
188340 KB |
Output is correct |
47 |
Correct |
94 ms |
188396 KB |
Output is correct |
48 |
Correct |
93 ms |
188352 KB |
Output is correct |
49 |
Correct |
104 ms |
188364 KB |
Output is correct |
50 |
Correct |
94 ms |
188320 KB |
Output is correct |
51 |
Correct |
93 ms |
188304 KB |
Output is correct |
52 |
Correct |
288 ms |
202764 KB |
Output is correct |
53 |
Correct |
310 ms |
207972 KB |
Output is correct |
54 |
Correct |
247 ms |
207072 KB |
Output is correct |
55 |
Correct |
246 ms |
207292 KB |
Output is correct |
56 |
Correct |
250 ms |
207692 KB |
Output is correct |
57 |
Correct |
251 ms |
207556 KB |
Output is correct |
58 |
Correct |
243 ms |
207988 KB |
Output is correct |
59 |
Correct |
241 ms |
207296 KB |
Output is correct |
60 |
Correct |
246 ms |
207264 KB |
Output is correct |
61 |
Correct |
238 ms |
207348 KB |
Output is correct |
62 |
Correct |
253 ms |
207260 KB |
Output is correct |
63 |
Correct |
214 ms |
206268 KB |
Output is correct |
64 |
Correct |
215 ms |
208068 KB |
Output is correct |
65 |
Correct |
301 ms |
208048 KB |
Output is correct |
66 |
Correct |
282 ms |
208044 KB |
Output is correct |
67 |
Correct |
297 ms |
207928 KB |
Output is correct |
68 |
Correct |
302 ms |
208052 KB |
Output is correct |
69 |
Correct |
269 ms |
207660 KB |
Output is correct |
70 |
Correct |
294 ms |
207884 KB |
Output is correct |
71 |
Correct |
276 ms |
207752 KB |
Output is correct |
72 |
Correct |
282 ms |
207780 KB |
Output is correct |
73 |
Correct |
275 ms |
207756 KB |
Output is correct |
74 |
Correct |
282 ms |
207852 KB |
Output is correct |
75 |
Correct |
289 ms |
207864 KB |
Output is correct |
76 |
Correct |
268 ms |
207940 KB |
Output is correct |
77 |
Correct |
240 ms |
207652 KB |
Output is correct |
78 |
Correct |
278 ms |
207964 KB |
Output is correct |
79 |
Correct |
287 ms |
207332 KB |
Output is correct |
80 |
Correct |
286 ms |
207424 KB |
Output is correct |