#include <bits/stdc++.h>
#define ll long long
#define lll __int128
#define db double
#define imx INT_MAX
#define imn INT_MIN
#define lmx LLONG_MAX
#define lmn LLONG_MIN
#define ld long double
#define lid id * 2 + 1
#define rid id * 2 + 2
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define ordered_llset tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
struct Line {
mutable ll m, c, p;
bool operator<(const Line& o) const {
return m < o.m;
}
bool operator<(ll x) const {
return p < x;
}
};
struct lineContainer : multiset<Line, less<>> {
ll div(ll a, ll b) {
return a / b - ((a ^ b) < 0 and a % b);
}
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = lmx, 0;
if (x->m == y->m) x->p = x->c > y->c ? lmx : lmn;
else x->p = div(y->c - x->c, x->m - y->m);
return x->p >= y->p;
}
void add(ll m, ll c) {
auto z = insert({m, c, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() and isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() and (--x)->p >= y->p) isect(x, erase(y));
}
ll eval(ll x) {
auto l = *lower_bound(x);
return l.m * x + l.c;
}
};
struct p_t {
ll d, c;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll n, m, t, x, w, i;
cin >> x >> n >> m >> w >> t;
n++;
vector<ll> s(n), d(m);
for (i = 0; i < n - 1; i++) cin >> s[i];
s[n - 1] = x;
vector<p_t> ppl(m);
for (i = 0; i < m; i++) cin >> ppl[i].d >> ppl[i].c;
sort(s.rbegin(), s.rend());
sort(ppl.begin(), ppl.end(), [](const auto& a, const auto& b){return a.d < b.d;});
for (i = 0; i < m; i++) d[i] = ppl[i].d;
vector<ll> pref(m + 1), last(m, lmx);
lineContainer lc;
for (i = 0; i < m; i++) pref[i + 1] = pref[i] + ppl[i].c;
int tmp;
for (i = 0; i < n; i++) {
tmp = upper_bound(d.begin(), d.end(), s[i] % t) - d.begin() - 1;
if (tmp >= 0) last[tmp] = s[i];
}
vector<ll> dp(m);
lc.add(0, 0);
for (i = 0; i < m; i++) {
if (i > 0) dp[i] += dp[i - 1];
dp[i] += ((x - d[i]) / t + 1) * w;
if (last[i] != lmx) dp[i] = min(dp[i], pref[i + 1] + last[i] / t * (i + 1) * w - lc.eval(last[i] / t * w));
lc.add(i + 1, pref[i + 1] - dp[i]);
}
cout << dp[m - 1] + (x / t + 1) * w;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
532 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
532 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
508 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
504 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
532 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
508 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
504 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
2 ms |
592 KB |
Output is correct |
39 |
Correct |
2 ms |
592 KB |
Output is correct |
40 |
Correct |
2 ms |
592 KB |
Output is correct |
41 |
Correct |
1 ms |
472 KB |
Output is correct |
42 |
Correct |
2 ms |
592 KB |
Output is correct |
43 |
Correct |
2 ms |
544 KB |
Output is correct |
44 |
Correct |
2 ms |
592 KB |
Output is correct |
45 |
Correct |
1 ms |
592 KB |
Output is correct |
46 |
Correct |
2 ms |
592 KB |
Output is correct |
47 |
Correct |
2 ms |
592 KB |
Output is correct |
48 |
Correct |
2 ms |
592 KB |
Output is correct |
49 |
Correct |
2 ms |
592 KB |
Output is correct |
50 |
Correct |
2 ms |
592 KB |
Output is correct |
51 |
Correct |
2 ms |
760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
1 ms |
532 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
508 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
504 KB |
Output is correct |
25 |
Correct |
1 ms |
336 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Correct |
1 ms |
336 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
464 KB |
Output is correct |
33 |
Correct |
1 ms |
336 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
2 ms |
592 KB |
Output is correct |
39 |
Correct |
2 ms |
592 KB |
Output is correct |
40 |
Correct |
2 ms |
592 KB |
Output is correct |
41 |
Correct |
1 ms |
472 KB |
Output is correct |
42 |
Correct |
2 ms |
592 KB |
Output is correct |
43 |
Correct |
2 ms |
544 KB |
Output is correct |
44 |
Correct |
2 ms |
592 KB |
Output is correct |
45 |
Correct |
1 ms |
592 KB |
Output is correct |
46 |
Correct |
2 ms |
592 KB |
Output is correct |
47 |
Correct |
2 ms |
592 KB |
Output is correct |
48 |
Correct |
2 ms |
592 KB |
Output is correct |
49 |
Correct |
2 ms |
592 KB |
Output is correct |
50 |
Correct |
2 ms |
592 KB |
Output is correct |
51 |
Correct |
2 ms |
760 KB |
Output is correct |
52 |
Correct |
116 ms |
16968 KB |
Output is correct |
53 |
Correct |
108 ms |
16968 KB |
Output is correct |
54 |
Correct |
91 ms |
16200 KB |
Output is correct |
55 |
Correct |
93 ms |
16456 KB |
Output is correct |
56 |
Correct |
96 ms |
16716 KB |
Output is correct |
57 |
Correct |
104 ms |
16756 KB |
Output is correct |
58 |
Correct |
90 ms |
16976 KB |
Output is correct |
59 |
Correct |
89 ms |
16456 KB |
Output is correct |
60 |
Correct |
89 ms |
16312 KB |
Output is correct |
61 |
Correct |
90 ms |
16460 KB |
Output is correct |
62 |
Correct |
97 ms |
16416 KB |
Output is correct |
63 |
Correct |
146 ms |
27964 KB |
Output is correct |
64 |
Correct |
83 ms |
17264 KB |
Output is correct |
65 |
Correct |
109 ms |
16968 KB |
Output is correct |
66 |
Correct |
103 ms |
17144 KB |
Output is correct |
67 |
Correct |
114 ms |
17016 KB |
Output is correct |
68 |
Correct |
108 ms |
17076 KB |
Output is correct |
69 |
Correct |
116 ms |
16712 KB |
Output is correct |
70 |
Correct |
114 ms |
16884 KB |
Output is correct |
71 |
Correct |
113 ms |
16712 KB |
Output is correct |
72 |
Correct |
125 ms |
16880 KB |
Output is correct |
73 |
Correct |
115 ms |
16712 KB |
Output is correct |
74 |
Correct |
118 ms |
16968 KB |
Output is correct |
75 |
Correct |
113 ms |
16832 KB |
Output is correct |
76 |
Correct |
115 ms |
16980 KB |
Output is correct |
77 |
Correct |
88 ms |
16756 KB |
Output is correct |
78 |
Correct |
96 ms |
16968 KB |
Output is correct |
79 |
Correct |
112 ms |
16456 KB |
Output is correct |
80 |
Correct |
113 ms |
16468 KB |
Output is correct |