//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 200005
#define pii pair<ll, ll>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const ll inf = 1LL<<60;
ll s[maxn];
pii a[maxn];
ll cost[maxn], pref[maxn];
struct line{
ll m, k;
line(){m = 0, k = -inf;}
line(ll a, ll b){m = a, k = b;}
ll getval(ll x){return m*x + k;}
};
vector<ll> vec[maxn];
ll dp[maxn];
int main() {
io
ll x, n, m, w, t;
cin >> x >> n >> m >> w >> t;
for (int i = 1;i <= n;i++) cin >> s[i];
sort(s + 1, s + 1 + n);
for (int i = 0;i < m;i++) {
cin >> a[i].ff >> a[i].ss;
}
sort(a, a + m);
for (int j = 0;j < m;j++) {
cost[j] = w * ((x - a[j].ff) / t + 1);
pref[j] = a[j].ss;
if (j) cost[j] += cost[j-1], pref[j] += pref[j-1];
}
s[n+1] = x;
for (int i = 0;i <= n;i++) {
ll rig = upper_bound(a, a + m, make_pair(s[i+1]%t-1, 1LL<<60)) - a - 1; //[lef, rig]
vec[rig].push_back(s[i+1]/t);
}
vector<line> conv;
conv.push_back(line(w, 0));
auto search = [&] (ll p) {
int low = 0, up = conv.size();
while (low < up - 1) {
int mid = (low + up) / 2;
if (conv[mid].getval(p) < conv[mid-1].getval(p)) low = mid;
else up = mid;
}
return conv[low].getval(p);
};
for (int i = 0;i < m;i++) {
if (i) dp[i] = min(dp[i], dp[i-1]);
for (auto h:vec[i]) {
dp[i] = min(dp[i], search(h) + pref[i] - cost[i] + h * w * i);
}
line add(-i*w, dp[i] + cost[i] - pref[i]);
while (conv.size() > 1) {
line p1 = conv[conv.size() - 2], p2 = conv.back();
ll pnt = (p2.k - p1.k + p1.m - p2.m - 1) / (p1.m - p2.m);
if (p2.getval(pnt) >= add.getval(pnt)) {
conv.pop_back();
} else {
break;
}
}
conv.push_back(add);
}
cout << dp[m-1] + cost[m-1] + (x / t + 1) * w<< endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4960 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
2 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4960 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
2 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
4940 KB |
Output is correct |
23 |
Correct |
3 ms |
4940 KB |
Output is correct |
24 |
Correct |
3 ms |
4940 KB |
Output is correct |
25 |
Correct |
3 ms |
4940 KB |
Output is correct |
26 |
Correct |
3 ms |
4940 KB |
Output is correct |
27 |
Correct |
3 ms |
4940 KB |
Output is correct |
28 |
Correct |
3 ms |
4940 KB |
Output is correct |
29 |
Correct |
3 ms |
4940 KB |
Output is correct |
30 |
Correct |
3 ms |
4940 KB |
Output is correct |
31 |
Correct |
3 ms |
4940 KB |
Output is correct |
32 |
Correct |
3 ms |
4940 KB |
Output is correct |
33 |
Correct |
3 ms |
4940 KB |
Output is correct |
34 |
Correct |
3 ms |
4940 KB |
Output is correct |
35 |
Correct |
3 ms |
4940 KB |
Output is correct |
36 |
Correct |
3 ms |
4940 KB |
Output is correct |
37 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4960 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
2 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
4940 KB |
Output is correct |
23 |
Correct |
3 ms |
4940 KB |
Output is correct |
24 |
Correct |
3 ms |
4940 KB |
Output is correct |
25 |
Correct |
3 ms |
4940 KB |
Output is correct |
26 |
Correct |
3 ms |
4940 KB |
Output is correct |
27 |
Correct |
3 ms |
4940 KB |
Output is correct |
28 |
Correct |
3 ms |
4940 KB |
Output is correct |
29 |
Correct |
3 ms |
4940 KB |
Output is correct |
30 |
Correct |
3 ms |
4940 KB |
Output is correct |
31 |
Correct |
3 ms |
4940 KB |
Output is correct |
32 |
Correct |
3 ms |
4940 KB |
Output is correct |
33 |
Correct |
3 ms |
4940 KB |
Output is correct |
34 |
Correct |
3 ms |
4940 KB |
Output is correct |
35 |
Correct |
3 ms |
4940 KB |
Output is correct |
36 |
Correct |
3 ms |
4940 KB |
Output is correct |
37 |
Correct |
3 ms |
4940 KB |
Output is correct |
38 |
Correct |
5 ms |
5068 KB |
Output is correct |
39 |
Correct |
4 ms |
5196 KB |
Output is correct |
40 |
Correct |
4 ms |
5196 KB |
Output is correct |
41 |
Correct |
4 ms |
5068 KB |
Output is correct |
42 |
Correct |
4 ms |
5068 KB |
Output is correct |
43 |
Correct |
4 ms |
5068 KB |
Output is correct |
44 |
Correct |
3 ms |
5068 KB |
Output is correct |
45 |
Correct |
4 ms |
5068 KB |
Output is correct |
46 |
Correct |
3 ms |
5196 KB |
Output is correct |
47 |
Correct |
4 ms |
5196 KB |
Output is correct |
48 |
Correct |
6 ms |
5068 KB |
Output is correct |
49 |
Correct |
5 ms |
5068 KB |
Output is correct |
50 |
Correct |
4 ms |
5068 KB |
Output is correct |
51 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4960 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
2 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
2 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
4940 KB |
Output is correct |
23 |
Correct |
3 ms |
4940 KB |
Output is correct |
24 |
Correct |
3 ms |
4940 KB |
Output is correct |
25 |
Correct |
3 ms |
4940 KB |
Output is correct |
26 |
Correct |
3 ms |
4940 KB |
Output is correct |
27 |
Correct |
3 ms |
4940 KB |
Output is correct |
28 |
Correct |
3 ms |
4940 KB |
Output is correct |
29 |
Correct |
3 ms |
4940 KB |
Output is correct |
30 |
Correct |
3 ms |
4940 KB |
Output is correct |
31 |
Correct |
3 ms |
4940 KB |
Output is correct |
32 |
Correct |
3 ms |
4940 KB |
Output is correct |
33 |
Correct |
3 ms |
4940 KB |
Output is correct |
34 |
Correct |
3 ms |
4940 KB |
Output is correct |
35 |
Correct |
3 ms |
4940 KB |
Output is correct |
36 |
Correct |
3 ms |
4940 KB |
Output is correct |
37 |
Correct |
3 ms |
4940 KB |
Output is correct |
38 |
Correct |
5 ms |
5068 KB |
Output is correct |
39 |
Correct |
4 ms |
5196 KB |
Output is correct |
40 |
Correct |
4 ms |
5196 KB |
Output is correct |
41 |
Correct |
4 ms |
5068 KB |
Output is correct |
42 |
Correct |
4 ms |
5068 KB |
Output is correct |
43 |
Correct |
4 ms |
5068 KB |
Output is correct |
44 |
Correct |
3 ms |
5068 KB |
Output is correct |
45 |
Correct |
4 ms |
5068 KB |
Output is correct |
46 |
Correct |
3 ms |
5196 KB |
Output is correct |
47 |
Correct |
4 ms |
5196 KB |
Output is correct |
48 |
Correct |
6 ms |
5068 KB |
Output is correct |
49 |
Correct |
5 ms |
5068 KB |
Output is correct |
50 |
Correct |
4 ms |
5068 KB |
Output is correct |
51 |
Correct |
4 ms |
5068 KB |
Output is correct |
52 |
Correct |
187 ms |
18124 KB |
Output is correct |
53 |
Correct |
151 ms |
20716 KB |
Output is correct |
54 |
Correct |
116 ms |
18116 KB |
Output is correct |
55 |
Correct |
115 ms |
18244 KB |
Output is correct |
56 |
Correct |
118 ms |
18192 KB |
Output is correct |
57 |
Correct |
117 ms |
18144 KB |
Output is correct |
58 |
Correct |
144 ms |
18104 KB |
Output is correct |
59 |
Correct |
120 ms |
18128 KB |
Output is correct |
60 |
Correct |
117 ms |
18156 KB |
Output is correct |
61 |
Correct |
122 ms |
18268 KB |
Output is correct |
62 |
Correct |
130 ms |
18156 KB |
Output is correct |
63 |
Correct |
94 ms |
24404 KB |
Output is correct |
64 |
Correct |
99 ms |
16224 KB |
Output is correct |
65 |
Correct |
155 ms |
20684 KB |
Output is correct |
66 |
Correct |
170 ms |
20716 KB |
Output is correct |
67 |
Correct |
160 ms |
20712 KB |
Output is correct |
68 |
Correct |
159 ms |
20660 KB |
Output is correct |
69 |
Correct |
166 ms |
18112 KB |
Output is correct |
70 |
Correct |
190 ms |
18116 KB |
Output is correct |
71 |
Correct |
174 ms |
18080 KB |
Output is correct |
72 |
Correct |
176 ms |
18168 KB |
Output is correct |
73 |
Correct |
170 ms |
18136 KB |
Output is correct |
74 |
Correct |
227 ms |
18176 KB |
Output is correct |
75 |
Correct |
171 ms |
18152 KB |
Output is correct |
76 |
Correct |
168 ms |
18096 KB |
Output is correct |
77 |
Correct |
118 ms |
18152 KB |
Output is correct |
78 |
Correct |
125 ms |
18312 KB |
Output is correct |
79 |
Correct |
173 ms |
18244 KB |
Output is correct |
80 |
Correct |
176 ms |
18156 KB |
Output is correct |