比赛 202103省实验桐柏一中普及组联赛 评测结果 AAAAAAAAAA
题目名称 自助者天助 最终得分 100
用户昵称 yrtiop 运行时间 0.289 s
代码语言 C++ 内存使用 7.07 MiB
提交时间 2021-03-22 19:55:57
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 30005;
int f[maxn];
int n,m;
int rk[maxn],v[maxn],w[maxn];
int w1[maxn],v1[maxn],num[maxn],sum = 0;
int W[maxn << 5],V[maxn << 5];
bool cmp(int a,int b) {
    return v[a] == v[b] ? w[a] < w[b] : v[a] < v[b];
}
int main() {
    freopen("delicious.in","r",stdin);
    freopen("delicious.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i = 1;i <= n;++ i) {
        scanf("%d%d",&w[i],&v[i]);
        rk[i] = i;
    }
    sort(rk + 1 , rk + 1 + n , cmp);
    for(int i = 1;i <= n;++ i) {
        int t = rk[i],s = rk[i - 1];
        if(v[s] != v[t]||w[s] != w[t]) {
            v1[++ sum] = v[t];
            w1[sum] = w[t];
            num[sum] = 1;
        }
        else {
            ++ num[sum];
        }
    }
    int cnt = 0;
    for(int i = 1;i <= sum;++ i) {
        int y = num[i],s = 1;
        while(y >= s) {
            V[++ cnt] = v1[i] * s;
            W[cnt] = w1[i] * s;
            y -= s;
            s <<= 1;
        }
        V[++ cnt] = v1[i] * y;
        W[cnt] = w1[i] * y;
    }
    for(int i = 1;i <= cnt;++ i) {
        for(int j = m;j >= W[i];-- j) {
            f[j] = max(f[j] , f[j - W[i]] + V[i]);
        }
    }
    printf("%d",f[m]);
    fclose(stdin);
    fclose(stdout);
    return 0;
}