#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 prefC[N];
int ame[N];
int f[N];
struct Line {
int a, b;
Line(int a = 0, int b = 0) : a(a), b(b) {}
double x(const Line& rhs) {
return 1.0 * (a - rhs.a) / (rhs.b - b);
}
};
namespace CHT {
int top;
Line line[N];
void add(Line nLine) {
while (top > 1 && line[top - 2].x(nLine) > line[top - 2].x(line[top - 1])) top -= 1;
line[top++] = nLine;
}
}
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;
prefC[i] = prefC[i - 1] + c[i];
}
}
memset(f, 14, sizeof f);
f[0] = 0;
for (int i = 1; i <= m; ++i) {
auto& ret = f[i];
CHT::add(Line(-i, f[i - 1] - prefC[i - 1]));
{ // take
ret = min(ret, f[i - 1] + ((x - d[i] - 1) / t + 1) * w);
}
if (ame[i]) { // no take
const int x = (s[ame[i]] - 1) / t * w;
auto cal = [&](int mid) {
return (i + CHT::line[mid].a + 1) * x + CHT::line[mid].b + prefC[i];
};
int l = 0, r = CHT::top - 1, it = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (cal(mid + 1) <= cal(mid)) l = it = mid + 1;
else r = mid - 1;
}
ret = min(ret, cal(it));
}
}
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... |