제출 #1038199

#제출 시각아이디문제언어결과실행 시간메모리
1038199AmirAli_H1Ski 2 (JOI24_ski2)C++17
100 / 100
105 ms4952 KiB
// In the name of Allah

#include <bits/stdc++.h>
using namespace std;

typedef		long long int			ll;
typedef		long double				ld;
typedef		pair<int, int>			pii;
typedef		pair<ll, ll>			pll;
typedef		complex<ld>				cld;

#define		all(x)					(x).begin(),(x).end()
#define		len(x)					((ll) (x).size())
#define		F						first
#define		S						second
#define		pb						push_back
#define		sep						' '
#define		endl					'\n'
#define		Mp						make_pair
#define		kill(x)					cout << x << '\n', exit(0)
#define		set_dec(x)				cout << fixed << setprecision(x);
#define		file_io(x,y)			freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n; ll k;
const int maxn = 600 + 4;
const ll oo = 1e18 + 4;
pll A[maxn]; vector<ll> arr;
ll dp[2][maxn][maxn], mn[maxn], cnt[maxn];

int GI(ll x) {
	return lower_bound(all(arr), x) - arr.begin();
}

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
	cin >> n >> k;
	for (int i = 0; i < n; i++) cin >> A[i].F >> A[i].S;
	
	sort(A, A + n); ll res = 0;
	for (int i = 1; i < n; i++) {
		if (A[i].F == A[0].F) {
			A[i].F++; res += k;
		}
	}
	sort(A, A + n);
	
	ll x = 0;
	for (int i = 0; i < n; i++) {
		x = max(x, A[i].F);
		arr.pb(x); x++;
	}
	
	fill(mn, mn + len(arr), oo);
	for (int i = 0; i < n; i++) {
		int j = GI(A[i].F);
		mn[j] = min(mn[j], A[i].S);
		cnt[j]++;
	}
	for (int i = 1; i < len(arr); i++) mn[i] = min(mn[i], mn[i - 1]);
	
	for (int i = 0; i < len(arr); i++) {
		if (i == 0) {
			for (int j1 = 0; j1 <= n; j1++) {
				for (int j2 = 0; j2 <= n; j2++) {
					dp[i & 1][j1][j2] = oo;
					if (j1 == 1 && j2 == 0) dp[i & 1][j1][j2] = 0;
				}
			}
		}
		else {
			for (int j1 = 0; j1 <= n; j1++) {
				for (int j2 = 1; j2 <= n; j2++) {
					dp[i & 1][j1 + 1][j2 - 1] = min(dp[i & 1][j1 + 1][j2 - 1], dp[i & 1][j1][j2] + mn[i - 1]);
				}
			}
		}
		if (i + 1 >= len(arr)) continue;
		
		for (int j1 = 0; j1 <= n; j1++) {
			for (int j2 = 0; j2 <= n; j2++) {
				dp[(i + 1) & 1][j1][j2] = oo;
			}
		}
		for (int j1 = 0; j1 <= n; j1++) {
			for (int j2 = 0; j2 <= n; j2++) {
				if (dp[i & 1][j1][j2] == oo) continue;
				ll x = dp[i & 1][j1][j2] + (k * (arr[i + 1] - arr[i]) * j2);
				int r1 = j1, r2 = j2 + cnt[i + 1];
				int e = min(r1, r2); r2 -= e;
				dp[(i + 1) & 1][r1][r2] = min(dp[(i + 1) & 1][r1][r2], x);
			}
		}
	}
	
	ll ans = oo;
	for (int j1 = 0; j1 <= n; j1++) ans = min(ans, dp[(len(arr) - 1) & 1][j1][0]);
	ans += res;
	
	cout << ans << endl;
	
	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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...