Description:

Eddy was a contestant participating in ACM ICPC contests. ACM is short for Algorithm, Coding, Math. Since in the ACM contest, the most important knowledge is about algorithm, followed by coding(implementation ability), then math. However, in the ACM ICPC World Finals 2018, Eddy failed to solve a physics equation, which pushed him away from a potential medal.

Since then on, Eddy found that physics is actually the most important thing in the contest. Thus, he wants to form a team to guide the following contestants to conquer the PACM contests(PACM is short for Physics, Algorithm, Coding, Math).

There are N candidate groups each composed of pi physics experts, ai algorithm experts, ci coding experts, mi math experts. For each group, Eddy can either invite all of them or none of them. If i-th team is invited, they will bring gi knowledge points which is calculated by Eddy’s magic formula. Eddy believes that the higher the total knowledge points is, the better a team could place in a contest. But, Eddy doesn’t want too many experts in the same area in the invited groups. Thus, the number of invited physics experts should not exceed P, and A for algorithm experts, C for coding experts, M for math experts.

Eddy is still busy in studying Physics. You come to help him to figure out which groups should be invited such that they doesn’t exceed the constraint and will bring the most knowledge points in total.

Input:

The first line contains a positive integer N indicating the number of candidate groups.
Each of following N lines contains five space-separated integer pi, ai, ci, mi, gi indicating that i-th team consists of pi physics experts, ai algorithm experts, ci coding experts, mi math experts, and will bring gi knowledge points.
The last line contains four space-separated integer P, A, C, M indicating the maximum possible number of physics experts, algorithm experts, coding experts, and math experts, respectively.

1 ≤ N ≤ 36
0 ≤ pi,ai,ci,mi,gi ≤ 36
0 ≤ P, A, C, M ≤ 36

Output:

The first line should contain a non-negative integer K indicating the number of invited groups.
The second line should contain K space-separated integer indicating the index of invited groups(groups are indexed from 0).

You can output index in any order as long as each index appears at most once. If there are multiple way to reach the most total knowledge points, you can output any one of them. If none of the groups will be invited, you could either output one line or output a blank line in the second line.

Sample Input:

2
1 0 2 1 10
1 0 2 1 21
1 0 2 1

Sample Output:

1
1

Sample Input:

1
2 1 1 0 31
1 0 2 1

Sample Output:

0

题目链接

题目是有四个限制条件的01背包问题,开五维int数组会超内存,五维short也只能卡数据边界开,所以降一维最为保险,在更新状态时用一个path数组记录,最后倒推求出选择情况。

#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF = 0x3f3f3f3f;
const int maxn = 37;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = asin(1.0) * 2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
 
struct Group {
    int p, a, c, m, g;
    Group(int _p = 0, int _a = 0, int _c = 0, int _m = 0, int _g = 0): p(_p), a(_a), c(_c), m(_m), g(_g) {}
};
 
int n;
int P, A, C, M;
vector<Group> groups(1);
short dp[maxn][maxn][maxn][maxn];
bool path[maxn][maxn][maxn][maxn][maxn];
vector<int> ans;
 
int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    read(n);
    for (int i = 0, p, a, c, m, g; i < n; ++i) {
        read(p); read(a); read(c); read(m); read(g);
        groups.pb(Group {p, a, c, m, g});
    }
    read(P); read(A); read(C); read(M);
    mem(dp, 0);
    mem(path, 0);
    for (int i = 1; i <= n; ++i) {
        for (int j = P; j >= groups[i].p; --j) {
                for (int k = A; k >= groups[i].a; --k) {
                        for (int l = C; l >= groups[i].c; --l) {
                                for (int o = M; o >= groups[i].m; --o) {
                                        if (dp[j - groups[i].p][k - groups[i].a][l - groups[i].c][o - groups[i].m] + groups[i].g > dp[j][k][l][o]) {
                                            dp[j][k][l][o] = dp[j - groups[i].p][k - groups[i].a][l - groups[i].c][o - groups[i].m] + groups[i].g;
                                            path[i][j][k][l][o] = 1;
                                        }
                                }
                        }
                }
        }
    }
    for (int i = n, j = P, k = A, l = C, o = M; i >= 1 && j >= 0 && k >= 0 && l >= 0 && o >= 0; --i) {
        if (path[i][j][k][l][o]) {
            j -= groups[i].p;
            k -= groups[i].a;
            l -= groups[i].c;
            o -= groups[i].m;
        }
    }
    printf("%d\n", int(ans.size()));
    reverse(ans.begin(), ans.end());
    for (auto it : ans) {
        printf("%d ", it);
    }
    printf("\n");
#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    system("gedit out.txt");
#endif
    return 0;
}