Submission #412661

#TimeUsernameProblemLanguageResultExecution timeMemory
412661amoo_safarLong Distance Coach (JOI17_coach)C++17
100 / 100
157 ms20828 KiB
// vaziat meshki-ghermeze !
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ll Mod = 1000000007LL;
const int N = 2e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;

ll dp[N];
ll rif[N];
ll mn[N];

typedef pll Line;
vector< pair<ll, Line> > CHT;
ll Inter(Line A, Line B){
	return (A.S - B.S) / (B.F - A.F) + ((A.S - B.S) % (B.F - A.F) > 0);
}
void Add(Line A){
	while(!CHT.empty() && Inter(CHT.back().S, A) <= CHT.back().F)
		CHT.pop_back();
	CHT.pb({CHT.empty() ? -Inf : Inter(CHT.back().S, A), A});
}
ll Get(ll X){
	int idx = upper_bound(all(CHT), pair<ll, Line>(X, pll(Inf, Inf))) - CHT.begin();
	idx --;
	return CHT[idx].S.F * X + CHT[idx].S.S;
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	ll X, n, m, W, T;
	cin >> X >> n >> m >> W >> T;
	
	vector<pll> D = { {0, 0} }; // D, C
	for(int i = 0; i < n; i++)
		cin >> rif[i];
	rif[n] = X;

	int d, c;
	for(int i = 0; i < m; i++){
		cin >> d >> c;
		D.pb({d, c});
	}
	sort(all(D));
	for(int i = 1; i <= m; i++) D[i].S += D[i - 1].S;
	memset(mn, 31, sizeof mn);
	for(int i = 0; i <= n; i++){
		int idx = lower_bound(all(D), pll(rif[i] % T, -1)) - D.begin();
		idx --;
		assert(0 <= idx);
		mn[idx] = min(mn[idx], rif[i]);
	}
	dp[0] = ((X + T) / T) * W;
	for(int i = 1; i <= m; i++){
		Add({-(i - 1), dp[i - 1] - D[i - 1].S});
		dp[i] = dp[i - 1] + ( 1 + ((X - D[i].F) / T) ) * W;
		// debug(mn[i]);
		if(mn[i] >= Inf) continue;
		
		ll fac = mn[i] / T;
		// debug(fac);
		dp[i] = min(dp[i], D[i].S + i * fac * W + Get(fac * W));
	}

	//
	// for(auto [d, c] : D) cerr << "(" << d << ", " << c << ")\n";
	// for(int i = 0; i <= m; i++) cerr << dp[i] << ' ';
	// cerr << '\n';
	//
	cout << dp[m] << '\n';
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...