#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef long long ll;
#define int ll
const int N = 4e5 + 5;
const int inf = 1e18;
int MX;
struct line{
int m, b;
line(): m(0), b(0){ }
line(int m, int b): m(m), b(b){ }
int operator * (const int& x) const {
return x * m + b;
}
};
struct ST{
line tree[N << 2];
int q[N], sz;
void init(int sz_, vector<int>& Q){
for(int i=0;i<(N << 2);i++) tree[i] = line(MX, inf);
sz = sz_;
for(int i=0;i<sz;i++) q[i] = Q[i];
}
void add(line v, int lx, int rx, int x){
int m = (lx + rx) >> 1;
if(tree[x] * q[m] > v * q[m]) swap(tree[x], v);
if(lx == rx) return;
if(tree[x] * q[lx] <= v * q[lx]){
add(v, m + 1, rx, x << 1 | 1);
} else {
add(v, lx, m, x << 1);
}
} void add(int m, int b){
add(line(m, b), 0, sz - 1, 1);
}
int get(int i, int lx, int rx, int x){
if(lx == rx) return tree[x] * q[i];
int m = (lx + rx) >> 1;
if(i <= m) return min(tree[x] * q[i], get(i, lx, m, x << 1));
else return min(tree[x] * q[i], get(i, m + 1, rx, x << 1 | 1));
} int get(int i){
return get(i, 0, sz - 1, 1);
}
}tree;
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int X, n, m, w, t; cin >> X >> n >> m >> w >> t;
vector<int> s(n), d(m), c(m);
vector<ar<int, 2>> a;
MX = t;
for(int i=0;i<n;i++){
cin >> s[i];
a.push_back({s[i] % t, -(s[i] / t) * w});
s[i] = (s[i] / t) * w;
}
s.push_back((X / t) * w);
sort(s.begin(), s.end());
s.erase(unique(s.begin(), s.end()), s.end());
tree.init(s.size(), s);
for(int i=0;i<m;i++){
cin >> d[i] >> c[i];
a.push_back({d[i], c[i]});
}
a.push_back({X % t, -(X / t) * w});
a.push_back({0, 0});
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
int sz = a.size();
vector<int> pref(sz), cnt(sz), tot(sz);
for(int i=1;i<sz;i++){
pref[i] = pref[i-1], cnt[i] = cnt[i-1];
if(a[i][1] > 0){
tot[i] = X / t + (a[i][0] <= (X % t));
pref[i] += a[i][1];
cnt[i]++;
}
}
vector<int> dp(sz);
dp[0] = (X / t + 1) * w;
tree.add(0, dp[0]);
for(int i=1;i<sz;i++){
if(a[i][1] > 0){
dp[i] = dp[i-1] + tot[i] * w;
tree.add(-cnt[i], -pref[i] + dp[i]);
continue;
}
int s_ = -a[i][1];
int j = lower_bound(s.begin(), s.end(), s_) - s.begin();
//~ cout<<s[j]<<" "<<s_<<"\n";
dp[i] = pref[i] + s_ * cnt[i] + tree.get(j);
tree.add(-cnt[i], -pref[i] + dp[i]);
//~ for(int j=0;j<i;j++){
//~ cout<<j<<" "<<i<<" "<<dp[j]<<" "<<s * w * (cnt[i] - cnt[j])<<" "<<pref[i] - pref[j]<<"\n";
//~ dp[i] = min(dp[i], dp[j] + s_ * (cnt[i] - cnt[j]) + pref[i] - pref[j]);
//~ }
}
//~ for(int i=0;i<sz;i++){
//~ cout<<dp[i]<<" ";
//~ } cout<<"\n";
//~ for(int i=0;i<sz;i++){
//~ cout<<a[i][0]<<" ";
//~ } cout<<"\n";
//~ for(int i=0;i<sz;i++){
//~ cout<<a[i][1]<<" ";
//~ } cout<<"\n";
cout<<dp[sz - 1]<<"\n";
}
/*
99 99 198 297 396 396 486 576 565 547
0 1 2 3 4 5 6 7 8 9
0 -63 35 62 71 -90 32 29 -54 -45
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
25300 KB |
Output is correct |
2 |
Correct |
13 ms |
25300 KB |
Output is correct |
3 |
Correct |
16 ms |
25340 KB |
Output is correct |
4 |
Correct |
14 ms |
25300 KB |
Output is correct |
5 |
Correct |
13 ms |
25376 KB |
Output is correct |
6 |
Correct |
15 ms |
25408 KB |
Output is correct |
7 |
Correct |
14 ms |
25300 KB |
Output is correct |
8 |
Correct |
13 ms |
25300 KB |
Output is correct |
9 |
Correct |
14 ms |
25300 KB |
Output is correct |
10 |
Correct |
13 ms |
25300 KB |
Output is correct |
11 |
Correct |
16 ms |
25364 KB |
Output is correct |
12 |
Correct |
13 ms |
25300 KB |
Output is correct |
13 |
Correct |
13 ms |
25300 KB |
Output is correct |
14 |
Correct |
13 ms |
25380 KB |
Output is correct |
15 |
Correct |
13 ms |
25300 KB |
Output is correct |
16 |
Correct |
14 ms |
25428 KB |
Output is correct |
17 |
Correct |
13 ms |
25388 KB |
Output is correct |
18 |
Correct |
14 ms |
25300 KB |
Output is correct |
19 |
Correct |
13 ms |
25300 KB |
Output is correct |
20 |
Correct |
14 ms |
25376 KB |
Output is correct |
21 |
Correct |
14 ms |
25296 KB |
Output is correct |
22 |
Correct |
13 ms |
25300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
25300 KB |
Output is correct |
2 |
Correct |
13 ms |
25300 KB |
Output is correct |
3 |
Correct |
16 ms |
25340 KB |
Output is correct |
4 |
Correct |
14 ms |
25300 KB |
Output is correct |
5 |
Correct |
13 ms |
25376 KB |
Output is correct |
6 |
Correct |
15 ms |
25408 KB |
Output is correct |
7 |
Correct |
14 ms |
25300 KB |
Output is correct |
8 |
Correct |
13 ms |
25300 KB |
Output is correct |
9 |
Correct |
14 ms |
25300 KB |
Output is correct |
10 |
Correct |
13 ms |
25300 KB |
Output is correct |
11 |
Correct |
16 ms |
25364 KB |
Output is correct |
12 |
Correct |
13 ms |
25300 KB |
Output is correct |
13 |
Correct |
13 ms |
25300 KB |
Output is correct |
14 |
Correct |
13 ms |
25380 KB |
Output is correct |
15 |
Correct |
13 ms |
25300 KB |
Output is correct |
16 |
Correct |
14 ms |
25428 KB |
Output is correct |
17 |
Correct |
13 ms |
25388 KB |
Output is correct |
18 |
Correct |
14 ms |
25300 KB |
Output is correct |
19 |
Correct |
13 ms |
25300 KB |
Output is correct |
20 |
Correct |
14 ms |
25376 KB |
Output is correct |
21 |
Correct |
14 ms |
25296 KB |
Output is correct |
22 |
Correct |
13 ms |
25300 KB |
Output is correct |
23 |
Correct |
14 ms |
25300 KB |
Output is correct |
24 |
Correct |
13 ms |
25300 KB |
Output is correct |
25 |
Correct |
13 ms |
25380 KB |
Output is correct |
26 |
Correct |
15 ms |
25384 KB |
Output is correct |
27 |
Correct |
14 ms |
25300 KB |
Output is correct |
28 |
Correct |
14 ms |
25300 KB |
Output is correct |
29 |
Correct |
14 ms |
25300 KB |
Output is correct |
30 |
Correct |
13 ms |
25332 KB |
Output is correct |
31 |
Correct |
14 ms |
25384 KB |
Output is correct |
32 |
Correct |
14 ms |
25300 KB |
Output is correct |
33 |
Correct |
14 ms |
25396 KB |
Output is correct |
34 |
Correct |
15 ms |
25388 KB |
Output is correct |
35 |
Correct |
14 ms |
25392 KB |
Output is correct |
36 |
Correct |
14 ms |
25340 KB |
Output is correct |
37 |
Correct |
14 ms |
25296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
25300 KB |
Output is correct |
2 |
Correct |
13 ms |
25300 KB |
Output is correct |
3 |
Correct |
16 ms |
25340 KB |
Output is correct |
4 |
Correct |
14 ms |
25300 KB |
Output is correct |
5 |
Correct |
13 ms |
25376 KB |
Output is correct |
6 |
Correct |
15 ms |
25408 KB |
Output is correct |
7 |
Correct |
14 ms |
25300 KB |
Output is correct |
8 |
Correct |
13 ms |
25300 KB |
Output is correct |
9 |
Correct |
14 ms |
25300 KB |
Output is correct |
10 |
Correct |
13 ms |
25300 KB |
Output is correct |
11 |
Correct |
16 ms |
25364 KB |
Output is correct |
12 |
Correct |
13 ms |
25300 KB |
Output is correct |
13 |
Correct |
13 ms |
25300 KB |
Output is correct |
14 |
Correct |
13 ms |
25380 KB |
Output is correct |
15 |
Correct |
13 ms |
25300 KB |
Output is correct |
16 |
Correct |
14 ms |
25428 KB |
Output is correct |
17 |
Correct |
13 ms |
25388 KB |
Output is correct |
18 |
Correct |
14 ms |
25300 KB |
Output is correct |
19 |
Correct |
13 ms |
25300 KB |
Output is correct |
20 |
Correct |
14 ms |
25376 KB |
Output is correct |
21 |
Correct |
14 ms |
25296 KB |
Output is correct |
22 |
Correct |
13 ms |
25300 KB |
Output is correct |
23 |
Correct |
14 ms |
25300 KB |
Output is correct |
24 |
Correct |
13 ms |
25300 KB |
Output is correct |
25 |
Correct |
13 ms |
25380 KB |
Output is correct |
26 |
Correct |
15 ms |
25384 KB |
Output is correct |
27 |
Correct |
14 ms |
25300 KB |
Output is correct |
28 |
Correct |
14 ms |
25300 KB |
Output is correct |
29 |
Correct |
14 ms |
25300 KB |
Output is correct |
30 |
Correct |
13 ms |
25332 KB |
Output is correct |
31 |
Correct |
14 ms |
25384 KB |
Output is correct |
32 |
Correct |
14 ms |
25300 KB |
Output is correct |
33 |
Correct |
14 ms |
25396 KB |
Output is correct |
34 |
Correct |
15 ms |
25388 KB |
Output is correct |
35 |
Correct |
14 ms |
25392 KB |
Output is correct |
36 |
Correct |
14 ms |
25340 KB |
Output is correct |
37 |
Correct |
14 ms |
25296 KB |
Output is correct |
38 |
Correct |
15 ms |
25560 KB |
Output is correct |
39 |
Correct |
15 ms |
25560 KB |
Output is correct |
40 |
Correct |
16 ms |
25560 KB |
Output is correct |
41 |
Correct |
16 ms |
25556 KB |
Output is correct |
42 |
Correct |
15 ms |
25592 KB |
Output is correct |
43 |
Correct |
15 ms |
25548 KB |
Output is correct |
44 |
Correct |
17 ms |
25620 KB |
Output is correct |
45 |
Correct |
16 ms |
25568 KB |
Output is correct |
46 |
Correct |
16 ms |
25684 KB |
Output is correct |
47 |
Correct |
16 ms |
25648 KB |
Output is correct |
48 |
Correct |
19 ms |
25656 KB |
Output is correct |
49 |
Correct |
17 ms |
25556 KB |
Output is correct |
50 |
Correct |
15 ms |
25556 KB |
Output is correct |
51 |
Correct |
17 ms |
25688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
25300 KB |
Output is correct |
2 |
Correct |
13 ms |
25300 KB |
Output is correct |
3 |
Correct |
16 ms |
25340 KB |
Output is correct |
4 |
Correct |
14 ms |
25300 KB |
Output is correct |
5 |
Correct |
13 ms |
25376 KB |
Output is correct |
6 |
Correct |
15 ms |
25408 KB |
Output is correct |
7 |
Correct |
14 ms |
25300 KB |
Output is correct |
8 |
Correct |
13 ms |
25300 KB |
Output is correct |
9 |
Correct |
14 ms |
25300 KB |
Output is correct |
10 |
Correct |
13 ms |
25300 KB |
Output is correct |
11 |
Correct |
16 ms |
25364 KB |
Output is correct |
12 |
Correct |
13 ms |
25300 KB |
Output is correct |
13 |
Correct |
13 ms |
25300 KB |
Output is correct |
14 |
Correct |
13 ms |
25380 KB |
Output is correct |
15 |
Correct |
13 ms |
25300 KB |
Output is correct |
16 |
Correct |
14 ms |
25428 KB |
Output is correct |
17 |
Correct |
13 ms |
25388 KB |
Output is correct |
18 |
Correct |
14 ms |
25300 KB |
Output is correct |
19 |
Correct |
13 ms |
25300 KB |
Output is correct |
20 |
Correct |
14 ms |
25376 KB |
Output is correct |
21 |
Correct |
14 ms |
25296 KB |
Output is correct |
22 |
Correct |
13 ms |
25300 KB |
Output is correct |
23 |
Correct |
14 ms |
25300 KB |
Output is correct |
24 |
Correct |
13 ms |
25300 KB |
Output is correct |
25 |
Correct |
13 ms |
25380 KB |
Output is correct |
26 |
Correct |
15 ms |
25384 KB |
Output is correct |
27 |
Correct |
14 ms |
25300 KB |
Output is correct |
28 |
Correct |
14 ms |
25300 KB |
Output is correct |
29 |
Correct |
14 ms |
25300 KB |
Output is correct |
30 |
Correct |
13 ms |
25332 KB |
Output is correct |
31 |
Correct |
14 ms |
25384 KB |
Output is correct |
32 |
Correct |
14 ms |
25300 KB |
Output is correct |
33 |
Correct |
14 ms |
25396 KB |
Output is correct |
34 |
Correct |
15 ms |
25388 KB |
Output is correct |
35 |
Correct |
14 ms |
25392 KB |
Output is correct |
36 |
Correct |
14 ms |
25340 KB |
Output is correct |
37 |
Correct |
14 ms |
25296 KB |
Output is correct |
38 |
Correct |
15 ms |
25560 KB |
Output is correct |
39 |
Correct |
15 ms |
25560 KB |
Output is correct |
40 |
Correct |
16 ms |
25560 KB |
Output is correct |
41 |
Correct |
16 ms |
25556 KB |
Output is correct |
42 |
Correct |
15 ms |
25592 KB |
Output is correct |
43 |
Correct |
15 ms |
25548 KB |
Output is correct |
44 |
Correct |
17 ms |
25620 KB |
Output is correct |
45 |
Correct |
16 ms |
25568 KB |
Output is correct |
46 |
Correct |
16 ms |
25684 KB |
Output is correct |
47 |
Correct |
16 ms |
25648 KB |
Output is correct |
48 |
Correct |
19 ms |
25656 KB |
Output is correct |
49 |
Correct |
17 ms |
25556 KB |
Output is correct |
50 |
Correct |
15 ms |
25556 KB |
Output is correct |
51 |
Correct |
17 ms |
25688 KB |
Output is correct |
52 |
Correct |
249 ms |
50128 KB |
Output is correct |
53 |
Correct |
260 ms |
56168 KB |
Output is correct |
54 |
Correct |
191 ms |
54884 KB |
Output is correct |
55 |
Correct |
191 ms |
55032 KB |
Output is correct |
56 |
Correct |
205 ms |
55320 KB |
Output is correct |
57 |
Correct |
192 ms |
55572 KB |
Output is correct |
58 |
Correct |
206 ms |
55796 KB |
Output is correct |
59 |
Correct |
197 ms |
54964 KB |
Output is correct |
60 |
Correct |
188 ms |
54948 KB |
Output is correct |
61 |
Correct |
189 ms |
55060 KB |
Output is correct |
62 |
Correct |
196 ms |
55188 KB |
Output is correct |
63 |
Correct |
179 ms |
54436 KB |
Output is correct |
64 |
Correct |
243 ms |
56176 KB |
Output is correct |
65 |
Correct |
261 ms |
56028 KB |
Output is correct |
66 |
Correct |
241 ms |
56008 KB |
Output is correct |
67 |
Correct |
250 ms |
56060 KB |
Output is correct |
68 |
Correct |
257 ms |
56060 KB |
Output is correct |
69 |
Correct |
292 ms |
55556 KB |
Output is correct |
70 |
Correct |
253 ms |
55640 KB |
Output is correct |
71 |
Correct |
256 ms |
55712 KB |
Output is correct |
72 |
Correct |
256 ms |
55664 KB |
Output is correct |
73 |
Correct |
264 ms |
55696 KB |
Output is correct |
74 |
Correct |
270 ms |
55688 KB |
Output is correct |
75 |
Correct |
264 ms |
55760 KB |
Output is correct |
76 |
Correct |
264 ms |
55876 KB |
Output is correct |
77 |
Correct |
136 ms |
54240 KB |
Output is correct |
78 |
Correct |
138 ms |
54480 KB |
Output is correct |
79 |
Correct |
251 ms |
55248 KB |
Output is correct |
80 |
Correct |
251 ms |
55224 KB |
Output is correct |