直接暴力
限定每种质量物品个数然后求价值前k大的质量
#include<bits/stdc++.h>
using namespace std;
#define N 4005
#define mp(a,b) make_pair(a,b)
#define fi first
#define se second
pair<int,int> p[N];
int n,k,id[N],tot,ed[N],sz[N],c[N],ans,op,ip[N];
bool cmp(int a,int b){return c[ip[a]]>c[ip[b]];}
bool cmp1(int a,int b){return sz[a]>sz[b];}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
scanf("%d%d",&n,&k);
for (int i=1,m,c;i<=n;i++) scanf("%d%d",&m,&c),p[i]=mp(m,c);
sort(p+1,p+n+1);
for (int i=n;i;i--)
{
if (p[i].fi!=p[i+1].fi) ++tot;
if (p[i-1].fi!=p[i].fi) ed[tot]=i;
c[tot]+=p[i].se;
sz[tot]++;
}
if (tot<k)
{
puts("0 0");
return 0;
}
for (int i=1;i<=tot;i++) ip[i]=i;
sort(ip+1,ip+tot+1,cmp1);
for (int i=k;i<=tot;i++)
{
if (ans>sz[ip[i]]) break;
for (int j=1;j<=i;j++) id[j]=j;
for (int j=1;j<i;j++) while (sz[ip[j]]>sz[ip[i]]) c[ip[j]]-=p[ed[ip[j]]++].se,sz[ip[j]]--;
sort(id+1,id+i+1,cmp);
int tmp=0;
for (int j=1;j<=k;j++) tmp+=c[ip[id[j]]];
ans=sz[ip[i]];op=max(op,tmp);
}
printf("%d %d\n",ans,op);
}