Submission #376723

#TimeUsernameProblemLanguageResultExecution timeMemory
376723fhvirusCake 3 (JOI19_cake3)C++17
0 / 100
1 ms876 KiB
// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; typedef pair<ll,ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define FOR(i,n) for(int i = 0; i < (n); ++i)
#define FOO(i,a,b) for(int i = (a); i <= (b); ++i)
#define AI(x) begin(x),end(x)
template<class I> bool chmax(I &a, I b){ return a < b ? (a = b, true) : false;}
template<class I> bool chmin(I &a, I b){ return a > b ? (a = b, true) : false;}
#ifdef OWO
#define debug(args...) LKJ("[ " + string(#args) + " ]", args)
void LKJ(){ cerr<<endl;}
template<class I, class...T> void LKJ(I&&x, T&&...t){ cerr<<x<<", ", LKJ(t...);}
template<class I> void DE(I a, I b){ while(a < b) cerr<<*a<<" \n"[next(a) == b], ++a;}
#else
#define debug(...) 0
#define DE(...) 0
#endif
const int N = 2e3 + 225;
int n, m;
struct cake{
	ll v, c;
	cake(ll v = 0, ll c = 0):
		v(v), c(c){}
	bool operator<(const cake &a){
		return c < a.c;
	}
} C[N];

ll dp[N][N];

int32_t main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> m;
	FOO(i,1,n){
		ll v, c;
		cin >> v >> c;
		C[i] = cake(v, c);
	}
	sort(C + 1, C + n + 1);
	FOR(i,n+1) FOR(j,n+1) dp[i][j] = -1e18;
	dp[0][0] = 0;
	FOO(i,1,n){
		dp[i][1] = C[i].v;
		FOO(j,2,min(i,m)){
			chmax(dp[i][j], dp[i-1][j-1] + C[i].v - 2 * (C[i].c - C[i-1].c));
			chmax(dp[i][j], dp[i-1][j] - 2 * (C[i].c - C[i-1].c));
		}
	}
	ll ans = -1e18;
	FOO(i,m,n) chmax(ans, dp[i][m]);
	cout << ans << '\n';
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...