#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
using ll = long long;
const ll mod=1e9+7;
const ll base=1e12;
const int nx=2e5+5;
int n, m;
ll x, w, t, s[nx], pre[nx], need[nx], dp[nx];
vector<pair<ll, ll>> a;
ll A(int i)
{
return -1ll*i*w;
}
ll B(int i)
{
return dp[i]-pre[i];
}
long double inter(int i, int j)
{
return (long double)(B(i)-B(j))/(A(j)-A(i));
}
bool check(int i, int j, int k)
{
//a1-a2/b2-b1
return inter(i, j)>=inter(j, k);
}
vector<int> f;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>x>>n>>m>>w>>t;
for(int i = 1; i <= n; i++)
cin>>s[i];
sort(s+1, s+n+1);
s[++n]=x;
for(int i = 1; i <= m; i++)
{
ll d, c;
cin>>d>>c;
a.emplace_back(d, c);
}
sort(a.begin(), a.end());
for(int i = 1; i <= m; i++)
pre[i]=pre[i-1]+a[i-1].se;
memset(need, -1, sizeof(need));
for(int i = 1; i <= n; i++)
{
int pos=upper_bound(a.begin(), a.end(), pair<ll, ll>(s[i]%t, x))-a.begin();
if(need[pos]==-1) need[pos]=s[i]/t;
}
for(int i = 0; i <= m; i++)
{
if(i>0)
{
ll d, c;
tie(d, c)=a[i-1];
dp[i]=dp[i-1]+1ll*((x-d)/t+1)*w;
// if(need[i]!=-1)
// for(int j = 0; j < i; j++)
// dp[i]=min(dp[i], need[i]*A(j)+B(j)+pre[i]+1ll*i*need[i]*w);
if(need[i]!=-1)
{
int d=1, c=f.size()-1, g, pos=0;
while(d<=c)
{
g=(d+c)>>1;
if(inter(f[g-1], f[g])<=(long double)need[i]) pos=g, d=g+1;
else c=g-1;
}
dp[i]=min(dp[i], need[i]*A(f[pos])+B(f[pos])+pre[i]+1ll*i*need[i]*w);
}
}
while(f.size()>1 && check(f[f.size()-2], f.back(), i)) f.pop_back();
f.emplace_back(i);
}
cout<<dp[m]+1ll*((x-1)/t+1)*w;
}
| # | 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... |