#include <bits/stdc++.h>
using namespace std;
#define int __int128
const int INF = 1e18+1e17;
struct line {
int m,k;
line(int m,int k):m(m),k(k){}
int operator()(int x) const {
return m*x+k;
}
};
struct node {
int L,R;
line best;
node *left,*right;
node(int L,int R):L(L),R(R),left(nullptr),right(nullptr),best(0,INF){}
};
void update(node* curr,line x) {
int mid = (curr->L+curr->R)/2;
bool betterL = x(curr->L)<curr->best(curr->L);
bool betterR = x(curr->R)<curr->best(curr->R);
bool betterM = x(mid)<curr->best(mid);
if(betterM) {
swap(curr->best,x);
update(curr,x);
} else if(betterL) {
if(curr->left==nullptr)curr->left=new node(curr->L,mid);
update(curr->left,x);
} else if(betterR) {
if(curr->right==nullptr)curr->right=new node(mid+1,curr->R);
update(curr->right,x);
}
}
int get(node* curr,int x) {
if(curr==nullptr)return INF;
int mid = (curr->L+curr->R)/2;
if(x<=mid)return min(curr->best(x),get(curr->left,x));
return min(curr->best(x),get(curr->right,x));
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
long long X,N,M,W,T;
cin >> X >> N >> M >> W >> T;
vector<tuple<int,int,int>> arr;
arr.emplace_back(0,0,0);
int iterations = X/T;
X%=T;
arr.emplace_back(X,2,iterations);
for(int i=1;i<=N;i++) {
long long S;cin>>S;
arr.emplace_back(S%T,2,S/T);
}
for(int i=1;i<=M;i++) {
long long D,C;cin>>D>>C;
arr.emplace_back(D,1,C);
}
sort(arr.begin(), arr.end());
vector<long long> DP(arr.size());
DP[0] = (iterations+1)*W;
node *lichao = new node(1,1e18);
int P = 0;
int pref = 0;
for(int i=1;i<arr.size();i++) {
auto [pos,type,c] = arr[i];
if(type==1) {
if(pos<X)DP[i]=(iterations+1)*W + DP[i-1];
else DP[i]=iterations*W + DP[i-1];
update(lichao,{-P,DP[i-1]-pref});
P++;
pref+=c;
continue;
}
DP[i]=DP[i-1];
DP[i]=min(DP[i],(long long)(get(lichao,c*W)+c*W*P+pref));
}
cout << DP.back() << '\n';
}
Compilation message
coach.cpp: In constructor 'node::node(__int128, __int128)':
coach.cpp:19:17: warning: 'node::right' will be initialized after [-Wreorder]
19 | node *left,*right;
| ^~~~~
coach.cpp:18:10: warning: 'line node::best' [-Wreorder]
18 | line best;
| ^~~~
coach.cpp:20:5: warning: when initialized here [-Wreorder]
20 | node(int L,int R):L(L),R(R),left(nullptr),right(nullptr),best(0,INF){}
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
424 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
472 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
424 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
472 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
2 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
2 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
600 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
3 ms |
796 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
2 ms |
608 KB |
Output is correct |
49 |
Correct |
3 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
604 KB |
Output is correct |
51 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
424 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
472 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
2 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
2 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
600 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
3 ms |
796 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
2 ms |
608 KB |
Output is correct |
49 |
Correct |
3 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
604 KB |
Output is correct |
51 |
Correct |
1 ms |
604 KB |
Output is correct |
52 |
Correct |
125 ms |
26724 KB |
Output is correct |
53 |
Correct |
151 ms |
30140 KB |
Output is correct |
54 |
Correct |
201 ms |
29868 KB |
Output is correct |
55 |
Correct |
199 ms |
30384 KB |
Output is correct |
56 |
Correct |
165 ms |
29544 KB |
Output is correct |
57 |
Correct |
160 ms |
30124 KB |
Output is correct |
58 |
Correct |
130 ms |
30224 KB |
Output is correct |
59 |
Correct |
207 ms |
30384 KB |
Output is correct |
60 |
Correct |
200 ms |
29636 KB |
Output is correct |
61 |
Correct |
230 ms |
30660 KB |
Output is correct |
62 |
Correct |
205 ms |
30648 KB |
Output is correct |
63 |
Correct |
259 ms |
46496 KB |
Output is correct |
64 |
Correct |
170 ms |
30380 KB |
Output is correct |
65 |
Correct |
141 ms |
30124 KB |
Output is correct |
66 |
Correct |
108 ms |
29864 KB |
Output is correct |
67 |
Correct |
180 ms |
29616 KB |
Output is correct |
68 |
Correct |
155 ms |
30128 KB |
Output is correct |
69 |
Correct |
145 ms |
29608 KB |
Output is correct |
70 |
Correct |
149 ms |
30316 KB |
Output is correct |
71 |
Correct |
149 ms |
30380 KB |
Output is correct |
72 |
Correct |
158 ms |
29860 KB |
Output is correct |
73 |
Correct |
140 ms |
30128 KB |
Output is correct |
74 |
Correct |
117 ms |
29872 KB |
Output is correct |
75 |
Correct |
110 ms |
29108 KB |
Output is correct |
76 |
Correct |
94 ms |
30012 KB |
Output is correct |
77 |
Correct |
257 ms |
29628 KB |
Output is correct |
78 |
Correct |
197 ms |
29796 KB |
Output is correct |
79 |
Correct |
131 ms |
29500 KB |
Output is correct |
80 |
Correct |
127 ms |
30204 KB |
Output is correct |