#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int MOD = 1000000007;
const ll INF = 4e18;
const int MX = 200005;
bool Q;
struct Line {
mutable ll k, m, p; // slope, y-intercept, last optimal x
bool operator<(const Line& o) const {
return Q ? p < o.p : k < o.k;
}
};
struct LineContainer : multiset<Line> {
const ll inf = LLONG_MAX;
ll div(ll a, ll b) { // floored division
if (b < 0) a *= -1, b *= -1;
if (a >= 0) return a/b;
return -((-a+b-1)/b);
}
// updates x->p, determines if y is unneeded
bool isect(iterator x, iterator y) {
if (y == end()) { x->p = inf; return 0; }
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(ll k, ll m) {
k *= -1, m *= -1;
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p) isect(x, erase(y));
}
ll query(ll x) { // gives max value
if (empty()) return INF;
Q = 1; auto l = *lb({0,0,x}); Q = 0;
return -(l.k * x + l.m);
}
};
LineContainer L;
ll X,N,M,W,T, cum[MX], dp[MX];
vpl D, S;
void mn(ll& a, ll b) { a = min(a,b); }
void input() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> X >> N >> M >> W >> T;
S.resize(N+1); F0R(i,N) cin >> S[i].s;
S[N].s = X;
F0R(i,N+1) S[i].f = S[i].s % T;
D.resize(M+1); F0R(i,M) cin >> D[i].f >> D[i].s;
D[M] = {T,0};
sort(all(S)), sort(all(D));
F0R(i,M+1) {
if (i) cum[i] = cum[i-1];
cum[i] += D[i].s;
}
}
int main() {
input();
int ind = N+1;
dp[M] = (X/T+1)*W;
F0Rd(i,M) {
ll val = min(L.query(i)-cum[i],dp[i+1]);
ll bes = INF;
while (ind && S[ind-1].f >= D[i].f) mn(bes,S[--ind].s/T*W);
if (bes != INF) L.add(-bes,val+bes*i+cum[i]);
dp[i] = val+((X-D[i].f)/T+1)*W;
}
ll val = min(L.query(-1),dp[0]);
cout << val;
}
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
* if you have no idea just guess the appropriate well-known algo instead of doing nothing :/
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
444 KB |
Output is correct |
4 |
Correct |
4 ms |
444 KB |
Output is correct |
5 |
Correct |
3 ms |
652 KB |
Output is correct |
6 |
Correct |
2 ms |
720 KB |
Output is correct |
7 |
Correct |
2 ms |
720 KB |
Output is correct |
8 |
Correct |
2 ms |
720 KB |
Output is correct |
9 |
Correct |
2 ms |
720 KB |
Output is correct |
10 |
Correct |
3 ms |
720 KB |
Output is correct |
11 |
Correct |
2 ms |
720 KB |
Output is correct |
12 |
Correct |
2 ms |
720 KB |
Output is correct |
13 |
Correct |
3 ms |
720 KB |
Output is correct |
14 |
Correct |
2 ms |
720 KB |
Output is correct |
15 |
Correct |
3 ms |
720 KB |
Output is correct |
16 |
Correct |
2 ms |
720 KB |
Output is correct |
17 |
Correct |
3 ms |
720 KB |
Output is correct |
18 |
Correct |
3 ms |
720 KB |
Output is correct |
19 |
Correct |
3 ms |
720 KB |
Output is correct |
20 |
Correct |
2 ms |
720 KB |
Output is correct |
21 |
Correct |
2 ms |
720 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
444 KB |
Output is correct |
4 |
Correct |
4 ms |
444 KB |
Output is correct |
5 |
Correct |
3 ms |
652 KB |
Output is correct |
6 |
Correct |
2 ms |
720 KB |
Output is correct |
7 |
Correct |
2 ms |
720 KB |
Output is correct |
8 |
Correct |
2 ms |
720 KB |
Output is correct |
9 |
Correct |
2 ms |
720 KB |
Output is correct |
10 |
Correct |
3 ms |
720 KB |
Output is correct |
11 |
Correct |
2 ms |
720 KB |
Output is correct |
12 |
Correct |
2 ms |
720 KB |
Output is correct |
13 |
Correct |
3 ms |
720 KB |
Output is correct |
14 |
Correct |
2 ms |
720 KB |
Output is correct |
15 |
Correct |
3 ms |
720 KB |
Output is correct |
16 |
Correct |
2 ms |
720 KB |
Output is correct |
17 |
Correct |
3 ms |
720 KB |
Output is correct |
18 |
Correct |
3 ms |
720 KB |
Output is correct |
19 |
Correct |
3 ms |
720 KB |
Output is correct |
20 |
Correct |
2 ms |
720 KB |
Output is correct |
21 |
Correct |
2 ms |
720 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
728 KB |
Output is correct |
24 |
Correct |
2 ms |
732 KB |
Output is correct |
25 |
Correct |
3 ms |
828 KB |
Output is correct |
26 |
Correct |
3 ms |
828 KB |
Output is correct |
27 |
Correct |
3 ms |
828 KB |
Output is correct |
28 |
Correct |
3 ms |
828 KB |
Output is correct |
29 |
Correct |
3 ms |
828 KB |
Output is correct |
30 |
Correct |
3 ms |
828 KB |
Output is correct |
31 |
Correct |
2 ms |
828 KB |
Output is correct |
32 |
Correct |
3 ms |
860 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
2 ms |
860 KB |
Output is correct |
35 |
Correct |
3 ms |
860 KB |
Output is correct |
36 |
Correct |
3 ms |
860 KB |
Output is correct |
37 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
444 KB |
Output is correct |
4 |
Correct |
4 ms |
444 KB |
Output is correct |
5 |
Correct |
3 ms |
652 KB |
Output is correct |
6 |
Correct |
2 ms |
720 KB |
Output is correct |
7 |
Correct |
2 ms |
720 KB |
Output is correct |
8 |
Correct |
2 ms |
720 KB |
Output is correct |
9 |
Correct |
2 ms |
720 KB |
Output is correct |
10 |
Correct |
3 ms |
720 KB |
Output is correct |
11 |
Correct |
2 ms |
720 KB |
Output is correct |
12 |
Correct |
2 ms |
720 KB |
Output is correct |
13 |
Correct |
3 ms |
720 KB |
Output is correct |
14 |
Correct |
2 ms |
720 KB |
Output is correct |
15 |
Correct |
3 ms |
720 KB |
Output is correct |
16 |
Correct |
2 ms |
720 KB |
Output is correct |
17 |
Correct |
3 ms |
720 KB |
Output is correct |
18 |
Correct |
3 ms |
720 KB |
Output is correct |
19 |
Correct |
3 ms |
720 KB |
Output is correct |
20 |
Correct |
2 ms |
720 KB |
Output is correct |
21 |
Correct |
2 ms |
720 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
728 KB |
Output is correct |
24 |
Correct |
2 ms |
732 KB |
Output is correct |
25 |
Correct |
3 ms |
828 KB |
Output is correct |
26 |
Correct |
3 ms |
828 KB |
Output is correct |
27 |
Correct |
3 ms |
828 KB |
Output is correct |
28 |
Correct |
3 ms |
828 KB |
Output is correct |
29 |
Correct |
3 ms |
828 KB |
Output is correct |
30 |
Correct |
3 ms |
828 KB |
Output is correct |
31 |
Correct |
2 ms |
828 KB |
Output is correct |
32 |
Correct |
3 ms |
860 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
2 ms |
860 KB |
Output is correct |
35 |
Correct |
3 ms |
860 KB |
Output is correct |
36 |
Correct |
3 ms |
860 KB |
Output is correct |
37 |
Correct |
3 ms |
860 KB |
Output is correct |
38 |
Correct |
5 ms |
916 KB |
Output is correct |
39 |
Correct |
5 ms |
960 KB |
Output is correct |
40 |
Correct |
4 ms |
1004 KB |
Output is correct |
41 |
Correct |
4 ms |
1060 KB |
Output is correct |
42 |
Correct |
5 ms |
1228 KB |
Output is correct |
43 |
Correct |
3 ms |
1228 KB |
Output is correct |
44 |
Correct |
5 ms |
1228 KB |
Output is correct |
45 |
Correct |
4 ms |
1260 KB |
Output is correct |
46 |
Correct |
3 ms |
1396 KB |
Output is correct |
47 |
Correct |
4 ms |
1396 KB |
Output is correct |
48 |
Correct |
4 ms |
1396 KB |
Output is correct |
49 |
Correct |
5 ms |
1400 KB |
Output is correct |
50 |
Correct |
6 ms |
1448 KB |
Output is correct |
51 |
Correct |
5 ms |
1488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
444 KB |
Output is correct |
4 |
Correct |
4 ms |
444 KB |
Output is correct |
5 |
Correct |
3 ms |
652 KB |
Output is correct |
6 |
Correct |
2 ms |
720 KB |
Output is correct |
7 |
Correct |
2 ms |
720 KB |
Output is correct |
8 |
Correct |
2 ms |
720 KB |
Output is correct |
9 |
Correct |
2 ms |
720 KB |
Output is correct |
10 |
Correct |
3 ms |
720 KB |
Output is correct |
11 |
Correct |
2 ms |
720 KB |
Output is correct |
12 |
Correct |
2 ms |
720 KB |
Output is correct |
13 |
Correct |
3 ms |
720 KB |
Output is correct |
14 |
Correct |
2 ms |
720 KB |
Output is correct |
15 |
Correct |
3 ms |
720 KB |
Output is correct |
16 |
Correct |
2 ms |
720 KB |
Output is correct |
17 |
Correct |
3 ms |
720 KB |
Output is correct |
18 |
Correct |
3 ms |
720 KB |
Output is correct |
19 |
Correct |
3 ms |
720 KB |
Output is correct |
20 |
Correct |
2 ms |
720 KB |
Output is correct |
21 |
Correct |
2 ms |
720 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
728 KB |
Output is correct |
24 |
Correct |
2 ms |
732 KB |
Output is correct |
25 |
Correct |
3 ms |
828 KB |
Output is correct |
26 |
Correct |
3 ms |
828 KB |
Output is correct |
27 |
Correct |
3 ms |
828 KB |
Output is correct |
28 |
Correct |
3 ms |
828 KB |
Output is correct |
29 |
Correct |
3 ms |
828 KB |
Output is correct |
30 |
Correct |
3 ms |
828 KB |
Output is correct |
31 |
Correct |
2 ms |
828 KB |
Output is correct |
32 |
Correct |
3 ms |
860 KB |
Output is correct |
33 |
Correct |
2 ms |
860 KB |
Output is correct |
34 |
Correct |
2 ms |
860 KB |
Output is correct |
35 |
Correct |
3 ms |
860 KB |
Output is correct |
36 |
Correct |
3 ms |
860 KB |
Output is correct |
37 |
Correct |
3 ms |
860 KB |
Output is correct |
38 |
Correct |
5 ms |
916 KB |
Output is correct |
39 |
Correct |
5 ms |
960 KB |
Output is correct |
40 |
Correct |
4 ms |
1004 KB |
Output is correct |
41 |
Correct |
4 ms |
1060 KB |
Output is correct |
42 |
Correct |
5 ms |
1228 KB |
Output is correct |
43 |
Correct |
3 ms |
1228 KB |
Output is correct |
44 |
Correct |
5 ms |
1228 KB |
Output is correct |
45 |
Correct |
4 ms |
1260 KB |
Output is correct |
46 |
Correct |
3 ms |
1396 KB |
Output is correct |
47 |
Correct |
4 ms |
1396 KB |
Output is correct |
48 |
Correct |
4 ms |
1396 KB |
Output is correct |
49 |
Correct |
5 ms |
1400 KB |
Output is correct |
50 |
Correct |
6 ms |
1448 KB |
Output is correct |
51 |
Correct |
5 ms |
1488 KB |
Output is correct |
52 |
Correct |
175 ms |
16504 KB |
Output is correct |
53 |
Correct |
150 ms |
22260 KB |
Output is correct |
54 |
Correct |
215 ms |
27296 KB |
Output is correct |
55 |
Correct |
181 ms |
32300 KB |
Output is correct |
56 |
Correct |
186 ms |
37656 KB |
Output is correct |
57 |
Correct |
180 ms |
42880 KB |
Output is correct |
58 |
Correct |
222 ms |
48712 KB |
Output is correct |
59 |
Correct |
182 ms |
53704 KB |
Output is correct |
60 |
Correct |
225 ms |
58688 KB |
Output is correct |
61 |
Correct |
192 ms |
63844 KB |
Output is correct |
62 |
Correct |
198 ms |
68836 KB |
Output is correct |
63 |
Correct |
330 ms |
85304 KB |
Output is correct |
64 |
Correct |
139 ms |
85304 KB |
Output is correct |
65 |
Correct |
215 ms |
85304 KB |
Output is correct |
66 |
Correct |
213 ms |
90332 KB |
Output is correct |
67 |
Correct |
175 ms |
96112 KB |
Output is correct |
68 |
Correct |
182 ms |
101864 KB |
Output is correct |
69 |
Correct |
178 ms |
107296 KB |
Output is correct |
70 |
Correct |
184 ms |
112748 KB |
Output is correct |
71 |
Correct |
191 ms |
118248 KB |
Output is correct |
72 |
Correct |
189 ms |
123996 KB |
Output is correct |
73 |
Correct |
196 ms |
129360 KB |
Output is correct |
74 |
Correct |
223 ms |
134788 KB |
Output is correct |
75 |
Correct |
223 ms |
140624 KB |
Output is correct |
76 |
Correct |
168 ms |
146360 KB |
Output is correct |
77 |
Correct |
195 ms |
151600 KB |
Output is correct |
78 |
Correct |
214 ms |
157232 KB |
Output is correct |
79 |
Correct |
225 ms |
162512 KB |
Output is correct |
80 |
Correct |
159 ms |
167588 KB |
Output is correct |