题源:https://www.luogu.org/problem/P1048

#include <bits/stdc++.h>

#define numm ch - 48
#define pd putchar(' ')
#define pn putchar('\n')
#define pb push_back
#define fi first
#define se second
#define fre1 freopen("1.txt", "r", stdin)
#define fre2 freopen("2.txt", "w", stdout)
typedef long long int ll;
typedef long long int LL;
using namespace std;

template<typename T>
void read(T &res) {
    bool flag = false;
    char ch;
    while (!isdigit(ch = getchar())) (ch == '-') && (flag = true);
    for (res = numm; isdigit(ch = getchar()); res = (res << 1) + (res << 3) + numm);
    flag && (res = -res);
}

template<typename T>
void write(T x) {
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

//static auto _ = []() {
//    ios::sync_with_stdio(false);
//    cin.tie(0);
//    return 0;
//}();
//#pragma GCC optimize(3, "Ofast", "inline")
//////////////////////////////////////////////////////////////////////////////////////////////

int t, m;
int w[105], v[105];

int mem[1005][105];

int dfs(int t, int x) {
    if (~mem[t][x]) return mem[t][x];
    if (x == m + 1) return 0;
    int ans1 = 0, ans2 = 0;
    if (t - w[x] >= 0)
        ans1 = dfs(t - w[x], x + 1) + v[x];
    ans2 = dfs(t, x + 1);
    return mem[t][x] = max(ans1, ans2);
}

int main() {
    read(t), read(m);
    for (int i = 1; i <= m; i++) {
        read(w[i]), read(v[i]);
    }
    memset(mem, -1, sizeof(mem));
    write(dfs(t, 1));
    return 0;
}
Last modification:November 15th, 2019 at 10:37 am
如果觉得我的文章对你有用,请随意赞赏