33,008
社区成员
发帖
与我相关
我的任务
分享
#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct Item {
int m;
int p;
};
struct CmpItem {
bool operator()(Item const &x, Item const &y) const {
return x.p < y.p;
}
};
Item g[17];
int N;
bool buy[20];
int restMoney;
int M, M1, M2;
int P;
int best;
int findGift() {
int i;
for (i = N - 1; i >= 0; --i) {
if (!buy[i] && g[i].m <= M2) return g[i].p;
}
return 0;
}
void solve(int x) {
if (x == N) {
int cost = M - restMoney;
if (cost >= M1) {
best = max(best, P + findGift());
} else {
best = max(best, P);
}
return;
}
buy[x] = false;
solve(x+1);
if (restMoney < g[x].m) return;
buy[x]=true;
restMoney -= g[x].m;
P += g[x].p;
solve(x+1);
restMoney += g[x].m;
P -= g[x].p;
}
void input() {
if (scanf("%d %d", &N, &M) != 2) exit(0);
int i;
for (i = 0; i < N; ++i) {
scanf("%d %d", &(g[i].m), &(g[i].p));
}
scanf("%d %d", &M1, &M2);
}
void run() {
sort(g, g + N, CmpItem());
best = -2000000000;
restMoney = M;
P = 0;
solve(0);
printf("%d\n", best);
}
int main() {
while(1) {
input();
run();
}
return 0;
}
#include <algorithm>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct Item {
int m;
int p;
};
struct CmpItem {
bool operator()(Item const &x, Item const &y) const {
return x.p < y.p;
}
};
Item g[17];
int N;
bool buy[20];
int restMoney;
int M, M1, M2;
int P;
int best;
int findGift() {
int i;
for (i = N - 1; i >= 0; --i) {
if (!buy[i] && g[i].m <= M2) return g[i].p;
}
return 0;
}
void solve(int x) {
if (x == N) {
int cost = M - restMoney;
if (cost >= M1) {
P += findGift();
}
best = max(best, P);
return;
}
buy[x] = false;
solve(x+1);
if (restMoney < g[x].m) return;
buy[x]=true;
restMoney -= g[x].m;
P += g[x].p;
solve(x+1);
restMoney -= g[x].m;
P -= g[x].p;
}
void input() {
if (scanf("%d %d", &N, &M) != 2) exit(0);
int i;
for (i = 0; i < N; ++i) {
scanf("%d %d", &(g[i].m), &(g[i].p));
}
scanf("%d %d", &M1, &M2);
}
void run() {
sort(g, g + N, CmpItem());
best = -2000000000;
restMoney = M;
P = 0;
solve(0);
printf("%d\n", best);
}
int main() {
while(1) {
input();
run();
}
return 0;
}