#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 200'000 + 10;
int x, n, m, w, t;
int s[N];
int d[N], c[N];
int ame[N];
int f[N];
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> x >> n >> m >> w >> t;
for (int i = 1; i <= n; ++i) cin >> s[i];
for (int i = 1; i <= m; ++i) cin >> d[i] >> c[i];
s[++n] = x;
sort(s + 1, s + n + 1);
{ // sort by D
vector<int> nD(d, d + m + 1), nC(c, c + m + 1);
vector<int> order(m); iota(order.begin(), order.end(), 1);
sort(order.begin(), order.end(), [&](const auto& a, const auto& b) {
return d[a] < d[b];
});
for (int i = 0; i < m; ++i) {
d[i + 1] = nD[order[i]];
c[i + 1] = nC[order[i]];
}
}
{ // cal ame
vector<pair<int, int>> vt;
for (int i = 1; i <= m; ++i) vt.push_back({d[i], i});
for (int i = 1; i <= n; ++i) vt.push_back({s[i] % t, -i});
sort(vt.begin(), vt.end());
fill(ame + 1, ame + m + 1, n + 2);
int last = 0;
for (const auto& [p, idx] : vt) {
if (idx > 0) last = idx;
else ame[last] = min(ame[last], -idx);
}
for (int i = 1; i <= m; ++i) if (ame[i] == n + 2) ame[i] = 0;
}
memset(f, 14, sizeof f);
f[0] = 0;
for (int i = 1; i <= m; ++i) {
auto& ret = f[i];
{ // take
ret = min(ret, f[i - 1] + ((x - d[i] - 1) / t + 1) * w);
}
if (ame[i]) { // no take
int sum = 0;
for (int j = 1; j <= i; ++j) {
sum += c[i - j + 1];
ret = min(ret, f[i - j] + (s[ame[i]] - 1) / t * w * j + sum);
}
}
}
int add = ((x - 1) / t + 1) * w;
cout << f[m] + add << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |