This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
const ll INF = 2e18;
int n, m;
ll X, T, W;
ll s[N];
ll d[N], c[N];
ll cost[N];
ll mn[N];
ll dp[N];
ll prefc[N];
struct seg {
int l, r;
ll x, sumcost, mnval;
};
struct cht {
struct line {
ll k, b;
};
bool bad(line l1, line l2, line l3) {
return __int128(l3.b - l1.b) * (l1.k - l2.k) <= __int128(l2.b - l1.b) * (l1.k - l3.k);
}
vector<line> st;
void add(ll k, ll b) {
line l = {k, b};
while (st.size() >= 2 && bad(st[st.size() - 2], st.back(), l)) {
st.pop_back();
}
st.push_back(l);
}
bool aft(line l1, line l2, ll x) {
return (l2.b - l1.b) <= __int128(x) * (l1.k - l2.k);
}
ll get(ll x) {
if (st.empty()) {
return INF;
}
int l = 0, r = st.size();
while (r - l > 1) {
int mid = (l + r) / 2;
if (aft(st[mid - 1], st[mid], x)) {
l = mid;
} else {
r = mid;
}
}
return st[l].k * x + st[l].b;
}
};
cht t[2 * N];
ll get(int l, int r, ll x) {
ll ret = INF;
for (l += m, r += m; l < r; l >>= 1, r >>= 1) {
if (l & 1) {
ret = min(ret, t[l].get(x));
++l;
}
if (r & 1) {
--r;
ret = min(ret, t[r].get(x));
}
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> X >> n >> m >> W >> T;
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
sort(s, s + n);
s[n++] = X;
{
vector<pair<ll, ll>> kek(m);
for (int i = 0; i < m; ++i) {
cin >> kek[i].first >> kek[i].second;
}
sort(kek.begin(), kek.end());
for (int i = 0; i < m; ++i) {
d[i] = kek[i].first;
c[i] = kek[i].second;
}
}
for (int i = 0; i < m; ++i) {
cost[i] = ((X - d[i]) / T + 1) * W;
}
for (int i = 0; i < m; ++i) {
mn[i] = INF;
}
for (int i = 0; i < n; ++i) {
int ind = lower_bound(d, d + m, s[i] % T) - d - 1;
if (ind != -1) {
mn[ind] = min(mn[ind], s[i] / T * W);
}
}
prefc[0] = 0;
for (int i = 0; i < m; ++i) {
prefc[i + 1] = prefc[i] + c[i];
}
vector<seg> st;
st.push_back({0, 0, -1, 0, INF});
for (int i = 0; i < m; ++i) {
for (int j = i + m; j > 0; j >>= 1) {
t[j].add(-i, dp[i] - prefc[i]);
}
dp[i + 1] = dp[i] + cost[i];
if (mn[i] != INF) {
while (st.back().x >= mn[i]) {
st.pop_back();
}
seg kek;
kek.l = st.back().r;
kek.r = i + 1;
kek.x = mn[i];
kek.sumcost = st.back().sumcost + kek.x * (kek.r - kek.l);
ll prefcost = st.back().sumcost;
kek.mnval = min(st.back().mnval, get(kek.l, kek.r, kek.x) - prefcost + kek.x * kek.l);
st.push_back(kek);
if (st.back().mnval != INF) {
dp[i + 1] = min(dp[i + 1], st.back().mnval + prefc[i + 1] + st.back().sumcost);
}
}
}
cout << dp[m] + (X / T + 1) * W << "\n";
return 0;
}
# | 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... |