Link
Solution
先按照 m i m_i mi排序
f i j f_{ij} fij表示在 i + 1... n i+1...n i+1...n这些人当中贿赂 j j j个人的情况下,让前 i i i个人都投我,最少花多少钱
Code
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 5050
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
ll n, m[maxn], p[maxn], f[maxn][maxn], id[maxn];
int main()
{
ll i, j, T=read();
auto upd = [](ll &x, ll y){x=min(x,y);};
while(T--)
{
n = read();
rep(i,1,n)m[i]=read(), p[i]=read(), id[i]=i;
rep(i,1,n)rep(j,0,n)f[i][j]=linf;
sort(id+1,id+n+1,[&](ll a, ll b){return m[a]<m[b];});
rep(i,0,n-1)rep(j,0,n)
{
upd(f[i+1][j-1],f[i][j]+p[id[i+1]]);
if(i+j>=m[id[i+1]])upd(f[i+1][j],f[i][j]);
}
printf("%lld\n",f[n][0]);
}
return 0;
}