#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 2e5+7;
const ll inf = 5e18;
bool Q = 0;
struct Line {
mutable ll m, b, x;
bool operator < (const Line ot) const {
return Q ? x < ot.x : m < ot.m;
}
};
ll ceil (ll a, ll b) {
if (a < 0 != b < 0) return a / b;
return (abs(a) + abs(b) - 1) / abs(b);
}
ll intersection (const Line &p, const Line &q) {
return ceil(q.b - p.b, p.m - q.m);
}
struct Hull : multiset<Line> {
bool valid (auto it) {
if (it == begin()) {
auto sig = it;
sig++;
if (sig != end()) sig->x = intersection(*it, *sig);
return it->x = -inf;
}
auto ant = it, sig = it;
ant--, sig++;
if (sig == end()) {
it->x = intersection(*it, *ant);
return 1;
}
ll x = intersection(*it, *ant);
ll y = intersection(*it, *sig);
if (x > y) return 0;
it->x = x, sig->x = y;
return 1;
}
void add (ll m, ll b) {
m = -m;
b = -b;
auto it = lower_bound({m, b, -inf});
if (it != end() && it->m == m) {
if (it->b > b) return;
it->b = b;
} else {
it = insert({m, b, -inf});
}
if (!valid(it)) {
erase(it);
return;
}
auto ant = it;
while (ant != begin()) {
if (valid(--ant)) break;
erase(ant);
if (it == begin()) { it->x = -inf; break; }
ant = it;
}
auto sig = it;
sig++;
while (sig != end() && !valid(sig))
erase(sig++);
}
ll query (ll x) {
if (empty()) return -inf;
Q = 1;
auto it = upper_bound({0, 0, x});
it--;
Q = 0;
return x * it->m + it->b;
}
} h;
int pref[N];
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int x, n, m, w, t;
cin >> x >> n >> m >> w >> t;
//first - d, second - c
vector <int> s(n);
for (int i = 0; i < n; ++i)
cin >> s[i];
sort(all(s));
s.app(x);
vector <ii> a(m);
for (int i = 0; i < m; ++i)
cin >> a[i].f >> a[i].s;
sort(all(a));
reverse(all(a));
a.insert(a.begin(), mp(0, 0));
map <int, int> pos;
for (int i = 0; i < a.size(); ++i)
pos[a[i].f] = i;
vector <int> c;
for (auto e : a)
c.app(e.f);
sort(all(c));
vector <int> last(s.size()), mem(a.size(), s.size());
for (int i = 0; i < s.size(); ++i) {
int p = lower_bound(all(c), s[i]%t) - c.begin();
p = (p - 1 + c.size()) % c.size();
last[i] = pos[c[p]];
mem[last[i]] = min(mem[last[i]], i);
}
vector <int> dp(a.size()+1);
dp[0] = (x/t+1)*w;
dp[1] = dp[0]+(x/t+(a[1].f<=(x%t)))*w;
for (int i = 0; i < a.size(); ++i)
pref[i+1] = pref[i] + a[i].s;
for (int j = 2; j <= a.size(); ++j) {
if (mem[j-1] != s.size()) {
int k = s[mem[j-1]]/t*w;
int x = -h.query(j-2);
if (x != inf) {
h.add(k, x-k*(j-2));
}
int i = j - 2;
h.add(k, dp[i]-pref[i+1]-i*k);
}
int save = 0;
if (j < a.size())
save = (x/t+(a[j].f<=(x%t)))*w;
int mn = -h.query(j-1)+pref[j];
mn = min(mn, dp[j-1]);
dp[j] = mn + save;
}
cout << (int)dp[a.size()] << endl;
}
Compilation message
coach.cpp: In function 'long long int ceil(long long int, long long int)':
coach.cpp:26:11: warning: suggest parentheses around comparison in operand of '!=' [-Wparentheses]
if (a < 0 != b < 0) return a / b;
~~^~~
coach.cpp: In function 'int main()':
coach.cpp:129:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a.size(); ++i)
~~^~~~~~~~~~
coach.cpp:138:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < s.size(); ++i) {
~~^~~~~~~~~~
coach.cpp:148:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a.size(); ++i)
~~^~~~~~~~~~
coach.cpp:151:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 2; j <= a.size(); ++j) {
~~^~~~~~~~~~~
coach.cpp:152:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (mem[j-1] != s.size()) {
coach.cpp:162:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (j < a.size())
~~^~~~~~~~~~
coach.cpp: In instantiation of 'bool Hull::valid(auto:1) [with auto:1 = std::_Rb_tree_const_iterator<Line>]':
coach.cpp:70:22: required from here
coach.cpp:38:29: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
return it->x = -inf;
^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
256 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
256 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
256 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
6 ms |
640 KB |
Output is correct |
39 |
Correct |
7 ms |
640 KB |
Output is correct |
40 |
Correct |
7 ms |
640 KB |
Output is correct |
41 |
Correct |
6 ms |
640 KB |
Output is correct |
42 |
Correct |
7 ms |
640 KB |
Output is correct |
43 |
Correct |
6 ms |
640 KB |
Output is correct |
44 |
Correct |
6 ms |
640 KB |
Output is correct |
45 |
Correct |
7 ms |
640 KB |
Output is correct |
46 |
Correct |
7 ms |
768 KB |
Output is correct |
47 |
Correct |
7 ms |
640 KB |
Output is correct |
48 |
Correct |
7 ms |
640 KB |
Output is correct |
49 |
Correct |
6 ms |
640 KB |
Output is correct |
50 |
Correct |
6 ms |
640 KB |
Output is correct |
51 |
Correct |
6 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
256 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
256 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
4 ms |
384 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
4 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
6 ms |
640 KB |
Output is correct |
39 |
Correct |
7 ms |
640 KB |
Output is correct |
40 |
Correct |
7 ms |
640 KB |
Output is correct |
41 |
Correct |
6 ms |
640 KB |
Output is correct |
42 |
Correct |
7 ms |
640 KB |
Output is correct |
43 |
Correct |
6 ms |
640 KB |
Output is correct |
44 |
Correct |
6 ms |
640 KB |
Output is correct |
45 |
Correct |
7 ms |
640 KB |
Output is correct |
46 |
Correct |
7 ms |
768 KB |
Output is correct |
47 |
Correct |
7 ms |
640 KB |
Output is correct |
48 |
Correct |
7 ms |
640 KB |
Output is correct |
49 |
Correct |
6 ms |
640 KB |
Output is correct |
50 |
Correct |
6 ms |
640 KB |
Output is correct |
51 |
Correct |
6 ms |
640 KB |
Output is correct |
52 |
Correct |
458 ms |
25912 KB |
Output is correct |
53 |
Correct |
464 ms |
31772 KB |
Output is correct |
54 |
Correct |
231 ms |
30876 KB |
Output is correct |
55 |
Correct |
239 ms |
31008 KB |
Output is correct |
56 |
Correct |
234 ms |
31264 KB |
Output is correct |
57 |
Correct |
241 ms |
31132 KB |
Output is correct |
58 |
Correct |
235 ms |
31552 KB |
Output is correct |
59 |
Correct |
242 ms |
31008 KB |
Output is correct |
60 |
Correct |
224 ms |
30756 KB |
Output is correct |
61 |
Correct |
244 ms |
31004 KB |
Output is correct |
62 |
Correct |
227 ms |
31004 KB |
Output is correct |
63 |
Correct |
365 ms |
42148 KB |
Output is correct |
64 |
Correct |
207 ms |
31920 KB |
Output is correct |
65 |
Correct |
509 ms |
31780 KB |
Output is correct |
66 |
Correct |
507 ms |
31644 KB |
Output is correct |
67 |
Correct |
480 ms |
31780 KB |
Output is correct |
68 |
Correct |
490 ms |
31748 KB |
Output is correct |
69 |
Correct |
487 ms |
31404 KB |
Output is correct |
70 |
Correct |
529 ms |
31516 KB |
Output is correct |
71 |
Correct |
484 ms |
31516 KB |
Output is correct |
72 |
Correct |
467 ms |
31396 KB |
Output is correct |
73 |
Correct |
485 ms |
31520 KB |
Output is correct |
74 |
Correct |
486 ms |
31524 KB |
Output is correct |
75 |
Correct |
470 ms |
31516 KB |
Output is correct |
76 |
Correct |
522 ms |
31776 KB |
Output is correct |
77 |
Correct |
219 ms |
31520 KB |
Output is correct |
78 |
Correct |
224 ms |
31516 KB |
Output is correct |
79 |
Correct |
479 ms |
31008 KB |
Output is correct |
80 |
Correct |
483 ms |
31112 KB |
Output is correct |