#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 205050
#define MAXS 20
#define INF (ll)2e18
#define bb ' '
#define ln '\n'
#define Ln '\n'
#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
#endif
#define MOD 1000000007
struct Line {
ll a, b;
Line(ll a = 0, ll b = INF) :a(a), b(b) {}
ll operator[](ll x) { return a * x + b; }
};
struct segtree {
struct Node {
ll l, r;
ll low, high;
Line f;
};
vector<Node> tree;
segtree(ll l, ll r) { tree.push_back({ -1, -1, l, r, Line() }); }
void upd(ll loc, Line f) {
ll l, r;
l = tree[loc].low;
r = tree[loc].high;
auto pv = tree[loc].f;
if (f[l] > pv[l]) swap(f, pv);
if (f[r] <= pv[r]) {
tree[loc].f = f;
return;
}
ll m = (l + r) >> 1;
if (f[m] > pv[m]) {
tree[loc].f = pv;
if (!~tree[loc].l) {
tree[loc].l = tree.size();
tree.push_back({ -1, -1, l, m, Line() });
}
upd(tree[loc].l, f);
}
else {
tree[loc].f = f;
if (!~tree[loc].r) {
tree[loc].r = tree.size();
tree.push_back({ -1, -1, m + 1, r, Line() });
}
upd(tree[loc].r, pv);
}
}
ll query(ll loc, ll x) {
if (!~loc) return INF;
ll m = (tree[loc].low + tree[loc].high) >> 1;
if (x <= m) return min(query(tree[loc].l, x), tree[loc].f[x]);
else return min(query(tree[loc].r, x), tree[loc].f[x]);
}
};
vector<ll> dps[MAX];
ll C[MAX];
ll dp[MAX];
ll S[MAX];
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
ll N, M;
ll W, X, T;
cin >> X >> N >> M >> W >> T;
ll i;
ll a, b;
vector<ll> s = { X };
for (i = 1; i <= N; i++) cin >> a, s.push_back(a);
vector<pll> in;
vector<ll> lst = { 0 };
for (i = 1; i <= M; i++) cin >> a >> b, in.emplace_back(a, b), lst.push_back(a);
sort(in.begin(), in.end());
for (i = 1; i <= M; i++) C[i] = in[i - 1].second;
sort(lst.begin(), lst.end());
for (auto x : s) dps[lower_bound(lst.begin(), lst.end(), x % T) - lst.begin() - 1].push_back(x / T);
for (i = 1; i <= M; i++) S[i] = S[i - 1] + C[i];
segtree seg(1, M);
for (i = M; i >= 1; i--) {
for (auto v : dps[i]) seg.upd(0, Line(-v * W, v * W * (ll)(i + 1) + dp[i + 1] + S[i]));
dp[i] = min(seg.query(0, i) - S[i - 1], dp[i + 1] + W * ((X - lst[i]) / T + 1));
}
cout << dp[1] + W * (X / T + 1) << ln;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
2 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
2 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
2 ms |
5148 KB |
Output is correct |
25 |
Correct |
2 ms |
5148 KB |
Output is correct |
26 |
Correct |
2 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5148 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5100 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
2 ms |
5152 KB |
Output is correct |
33 |
Correct |
3 ms |
5120 KB |
Output is correct |
34 |
Correct |
3 ms |
5084 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
2 ms |
5148 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
2 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
2 ms |
5148 KB |
Output is correct |
25 |
Correct |
2 ms |
5148 KB |
Output is correct |
26 |
Correct |
2 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5148 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5100 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
2 ms |
5152 KB |
Output is correct |
33 |
Correct |
3 ms |
5120 KB |
Output is correct |
34 |
Correct |
3 ms |
5084 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
2 ms |
5148 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5332 KB |
Output is correct |
39 |
Correct |
4 ms |
5460 KB |
Output is correct |
40 |
Correct |
4 ms |
5544 KB |
Output is correct |
41 |
Correct |
4 ms |
5332 KB |
Output is correct |
42 |
Correct |
4 ms |
5284 KB |
Output is correct |
43 |
Correct |
4 ms |
5332 KB |
Output is correct |
44 |
Correct |
4 ms |
5332 KB |
Output is correct |
45 |
Correct |
4 ms |
5332 KB |
Output is correct |
46 |
Correct |
3 ms |
5544 KB |
Output is correct |
47 |
Correct |
4 ms |
5404 KB |
Output is correct |
48 |
Correct |
4 ms |
5332 KB |
Output is correct |
49 |
Correct |
3 ms |
5284 KB |
Output is correct |
50 |
Correct |
4 ms |
5332 KB |
Output is correct |
51 |
Correct |
4 ms |
5332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
2 ms |
5076 KB |
Output is correct |
3 |
Correct |
2 ms |
5076 KB |
Output is correct |
4 |
Correct |
2 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
2 ms |
5076 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
2 ms |
5076 KB |
Output is correct |
13 |
Correct |
2 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
2 ms |
5076 KB |
Output is correct |
16 |
Correct |
2 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
2 ms |
5076 KB |
Output is correct |
19 |
Correct |
2 ms |
5076 KB |
Output is correct |
20 |
Correct |
2 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
2 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
2 ms |
5148 KB |
Output is correct |
25 |
Correct |
2 ms |
5148 KB |
Output is correct |
26 |
Correct |
2 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5148 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5100 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
2 ms |
5152 KB |
Output is correct |
33 |
Correct |
3 ms |
5120 KB |
Output is correct |
34 |
Correct |
3 ms |
5084 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
2 ms |
5148 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5332 KB |
Output is correct |
39 |
Correct |
4 ms |
5460 KB |
Output is correct |
40 |
Correct |
4 ms |
5544 KB |
Output is correct |
41 |
Correct |
4 ms |
5332 KB |
Output is correct |
42 |
Correct |
4 ms |
5284 KB |
Output is correct |
43 |
Correct |
4 ms |
5332 KB |
Output is correct |
44 |
Correct |
4 ms |
5332 KB |
Output is correct |
45 |
Correct |
4 ms |
5332 KB |
Output is correct |
46 |
Correct |
3 ms |
5544 KB |
Output is correct |
47 |
Correct |
4 ms |
5404 KB |
Output is correct |
48 |
Correct |
4 ms |
5332 KB |
Output is correct |
49 |
Correct |
3 ms |
5284 KB |
Output is correct |
50 |
Correct |
4 ms |
5332 KB |
Output is correct |
51 |
Correct |
4 ms |
5332 KB |
Output is correct |
52 |
Correct |
203 ms |
31704 KB |
Output is correct |
53 |
Correct |
143 ms |
33980 KB |
Output is correct |
54 |
Correct |
166 ms |
25528 KB |
Output is correct |
55 |
Correct |
169 ms |
25620 KB |
Output is correct |
56 |
Correct |
166 ms |
25796 KB |
Output is correct |
57 |
Correct |
186 ms |
26044 KB |
Output is correct |
58 |
Correct |
160 ms |
26112 KB |
Output is correct |
59 |
Correct |
162 ms |
25576 KB |
Output is correct |
60 |
Correct |
168 ms |
25564 KB |
Output is correct |
61 |
Correct |
165 ms |
25480 KB |
Output is correct |
62 |
Correct |
164 ms |
25688 KB |
Output is correct |
63 |
Correct |
137 ms |
41688 KB |
Output is correct |
64 |
Correct |
85 ms |
25992 KB |
Output is correct |
65 |
Correct |
143 ms |
34092 KB |
Output is correct |
66 |
Correct |
140 ms |
33344 KB |
Output is correct |
67 |
Correct |
142 ms |
34112 KB |
Output is correct |
68 |
Correct |
139 ms |
34072 KB |
Output is correct |
69 |
Correct |
211 ms |
31620 KB |
Output is correct |
70 |
Correct |
200 ms |
31628 KB |
Output is correct |
71 |
Correct |
205 ms |
31676 KB |
Output is correct |
72 |
Correct |
214 ms |
31584 KB |
Output is correct |
73 |
Correct |
206 ms |
31676 KB |
Output is correct |
74 |
Correct |
203 ms |
31124 KB |
Output is correct |
75 |
Correct |
200 ms |
28736 KB |
Output is correct |
76 |
Correct |
194 ms |
28496 KB |
Output is correct |
77 |
Correct |
159 ms |
25748 KB |
Output is correct |
78 |
Correct |
157 ms |
25932 KB |
Output is correct |
79 |
Correct |
200 ms |
31180 KB |
Output is correct |
80 |
Correct |
198 ms |
31252 KB |
Output is correct |