#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 1LL << 60;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
struct Line{
ll a = 0, b = MAX;
ll operator()(ll x){
return a * x + b;
}
};
vector<Line> st;
void modify(Line ln, int L, int R, int id){
int M = (L + R) / 2;
if(ln(M) < st[id](M)){
swap(ln, st[id]);
}
if(L == R) return;
if(ln.a >= st[id].a) modify(ln, L, M, 2 * id + 1);
else modify(ln, M + 1, R, 2 * id + 2);
}
void modify(int l, int r, Line ln, int L, int R, int id){
if(l == L && r == R){
modify(ln, L, R, id);
return;
}
int M = (L + R) / 2;
if(r <= M) modify(l, r, ln, L, M, 2 * id + 1);
else if(l > M) modify(l, r, ln, M + 1, R, 2 * id + 2);
else{
modify(l, M, ln, L, M, 2 * id + 1);
modify(M + 1, r, ln, M + 1, R, 2 * id + 2);
}
}
ll query(int pos, int L, int R, int id){
if(L == R) return st[id](pos);
int M = (L + R) / 2;
if(pos <= M) return min(st[id](pos), query(pos, L, M, 2 * id + 1));
else return min(st[id](pos), query(pos, M + 1, R, 2 * id + 2));
}
int main(){
StarBurstStream
ll X, T, W;
int n, m;
cin >> X >> n >> m >> W >> T;
st.resize(4 * (m + 1));
vector<ll> s(n + 1);
for(int i = 0; i < n; i++) cin >> s[i];
s[n] = X;
lsort(s);
vector<pll> tmp(m + 1);
for(int i = 1; i <= m; i++){
cin >> tmp[i].F >> tmp[i].S;
}
lsort(tmp);
vector<ll> D(m + 1), C(m + 1);
for(int i = 1; i <= m; i++){
D[i] = tmp[i].F;
C[i] = tmp[i].S;
}
vector<vector<ll>> sp(m + 2);
for(ll i : s){
int id = upper_bound(iter(D), i % T) - D.begin();
id--;
// cerr << "sp " << i << " " << id + 1 << "\n";
sp[id + 1].eb(i);
}
vector<ll> dp(m + 2);
for(int i = 1; i <= m; i++) dp[m + 1] += C[i];
dp[m + 1] += iceil(X, T) * W;
// cerr << "test " << m + 1 << " " << dp[m + 1] << "\n";
for(ll i : sp[m + 1]){
ll t = i / T;
auto it = lower_bound(iter(s), i);
int lst;
if(it == s.begin()) lst = 1;
else{
it--;
if(i - *it >= T) lst = 1;
else{
int id = upper_bound(iter(D), *it) - D.begin();
if(id >= m + 1) lst = 1;
else lst = id;
}
}
modify(lst, m + 1, Line({- t * W, (m + 1) * t * W + dp[m + 1]}), 1, m + 1, 0);
}
for(int pos = m; pos >= 1; pos--){
ll cnt = X / T;
if(X % T > D[pos]) cnt++;
dp[pos] = min(dp[pos + 1] + cnt * W - C[pos], query(pos, 1, m + 1, 0));
// cerr << "test " << pos << " " << cnt << " " << dp[pos] << " " << dp[pos + 1] + cnt * W - C[pos] << " " << query(pos, 1, m + 1, 0) << "\n";
for(ll i : sp[pos]){
ll t = i / T;
auto it = lower_bound(iter(s), i);
int lst;
if(it == s.begin()) lst = 1;
else{
it--;
if(i - *it >= T) lst = 1;
else{
int id = upper_bound(iter(D), *it) - D.begin();
if(id >= pos) lst = 1;
else lst = id;
}
}
modify(lst, pos, Line({- t * W, pos * t * W + dp[pos]}), 1, m + 1, 0);
}
}
cout << dp[1] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
216 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
312 KB |
Output is correct |
32 |
Correct |
1 ms |
312 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
308 KB |
Output is correct |
35 |
Correct |
1 ms |
312 KB |
Output is correct |
36 |
Correct |
1 ms |
308 KB |
Output is correct |
37 |
Correct |
1 ms |
312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
216 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
312 KB |
Output is correct |
32 |
Correct |
1 ms |
312 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
308 KB |
Output is correct |
35 |
Correct |
1 ms |
312 KB |
Output is correct |
36 |
Correct |
1 ms |
308 KB |
Output is correct |
37 |
Correct |
1 ms |
312 KB |
Output is correct |
38 |
Correct |
3 ms |
588 KB |
Output is correct |
39 |
Correct |
3 ms |
588 KB |
Output is correct |
40 |
Correct |
4 ms |
588 KB |
Output is correct |
41 |
Correct |
3 ms |
588 KB |
Output is correct |
42 |
Correct |
3 ms |
588 KB |
Output is correct |
43 |
Correct |
2 ms |
580 KB |
Output is correct |
44 |
Correct |
3 ms |
588 KB |
Output is correct |
45 |
Correct |
3 ms |
588 KB |
Output is correct |
46 |
Correct |
2 ms |
588 KB |
Output is correct |
47 |
Correct |
3 ms |
588 KB |
Output is correct |
48 |
Correct |
3 ms |
588 KB |
Output is correct |
49 |
Correct |
3 ms |
588 KB |
Output is correct |
50 |
Correct |
3 ms |
588 KB |
Output is correct |
51 |
Correct |
3 ms |
660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
216 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
204 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
312 KB |
Output is correct |
32 |
Correct |
1 ms |
312 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
308 KB |
Output is correct |
35 |
Correct |
1 ms |
312 KB |
Output is correct |
36 |
Correct |
1 ms |
308 KB |
Output is correct |
37 |
Correct |
1 ms |
312 KB |
Output is correct |
38 |
Correct |
3 ms |
588 KB |
Output is correct |
39 |
Correct |
3 ms |
588 KB |
Output is correct |
40 |
Correct |
4 ms |
588 KB |
Output is correct |
41 |
Correct |
3 ms |
588 KB |
Output is correct |
42 |
Correct |
3 ms |
588 KB |
Output is correct |
43 |
Correct |
2 ms |
580 KB |
Output is correct |
44 |
Correct |
3 ms |
588 KB |
Output is correct |
45 |
Correct |
3 ms |
588 KB |
Output is correct |
46 |
Correct |
2 ms |
588 KB |
Output is correct |
47 |
Correct |
3 ms |
588 KB |
Output is correct |
48 |
Correct |
3 ms |
588 KB |
Output is correct |
49 |
Correct |
3 ms |
588 KB |
Output is correct |
50 |
Correct |
3 ms |
588 KB |
Output is correct |
51 |
Correct |
3 ms |
660 KB |
Output is correct |
52 |
Correct |
362 ms |
36272 KB |
Output is correct |
53 |
Correct |
354 ms |
38980 KB |
Output is correct |
54 |
Correct |
307 ms |
35652 KB |
Output is correct |
55 |
Correct |
293 ms |
35652 KB |
Output is correct |
56 |
Correct |
298 ms |
36068 KB |
Output is correct |
57 |
Correct |
289 ms |
35780 KB |
Output is correct |
58 |
Correct |
301 ms |
36368 KB |
Output is correct |
59 |
Correct |
318 ms |
35752 KB |
Output is correct |
60 |
Correct |
308 ms |
35480 KB |
Output is correct |
61 |
Correct |
303 ms |
35652 KB |
Output is correct |
62 |
Correct |
294 ms |
35636 KB |
Output is correct |
63 |
Correct |
250 ms |
37316 KB |
Output is correct |
64 |
Correct |
169 ms |
34348 KB |
Output is correct |
65 |
Correct |
372 ms |
38944 KB |
Output is correct |
66 |
Correct |
360 ms |
38944 KB |
Output is correct |
67 |
Correct |
362 ms |
38928 KB |
Output is correct |
68 |
Correct |
344 ms |
39024 KB |
Output is correct |
69 |
Correct |
364 ms |
36160 KB |
Output is correct |
70 |
Correct |
436 ms |
36156 KB |
Output is correct |
71 |
Correct |
386 ms |
36164 KB |
Output is correct |
72 |
Correct |
366 ms |
36036 KB |
Output is correct |
73 |
Correct |
358 ms |
36296 KB |
Output is correct |
74 |
Correct |
362 ms |
36336 KB |
Output is correct |
75 |
Correct |
405 ms |
36292 KB |
Output is correct |
76 |
Correct |
380 ms |
36272 KB |
Output is correct |
77 |
Correct |
278 ms |
36156 KB |
Output is correct |
78 |
Correct |
378 ms |
36260 KB |
Output is correct |
79 |
Correct |
357 ms |
35780 KB |
Output is correct |
80 |
Correct |
356 ms |
35760 KB |
Output is correct |