最后一组数据之后不要有空行
WA :
// UVA990
//
// by wanglingZlZ
#include<cstdio>
#include<cstring>
#include<vector>
#define LL long long
#define INF (1e9+7)
const int MAXN=1e5+5;
int w[35],v[35],f[1005];
bool flag,b[35][1005];
int main() {
// freopen("Diving_for_Gold.txt","w",stdout);
int t,ww,n;
while (scanf("%d%d\n%d",&t,&ww,&n)==3) {
memset(w,0,sizeof(w));
memset(v,0,sizeof(v));
memset(f,0,sizeof(f));
memset(b,0,sizeof(b));
// printf("%d\n",n);
for (int i=1;i<=n;i++) {
scanf("%d %d",&v[i],&w[i]);
v[i]*=ww*3;
// printf("v:%d ",v[i]);
}
for (int i=1;i<=n;i++) {
for (int j=t;j>=v[i];j--) {
if (f[j-v[i]]+w[i]>f[j]) {
f[j]=f[j-v[i]]+w[i];
// puts("Yes");
b[i][j]=true;
}
}
}
// for (int i=1;i<=t;i++) {
// printf("f[%d]=%d\n",i,f[i]);
// }
int now=t;
std::vector<int> u;
for (int i=n;i>=1;i--) {
if (b[i][now]) {
u.push_back(i);
now-=v[i];
}
}
printf("%d\n%d\n",f[t],u.size());
for (int i=u.size()-1;i>=0;i--) {
printf("%d %d\n",v[u[i]]/3/ww,w[u[i]]);
}
puts("");
}
return 0;
}
//
//
//
// 210 4
// 3
// 10 5
// 10 1
// 7 2
AC :
// UVA990
//
// by wanglingZlZ
#include<cstdio>
#include<cstring>
#include<vector>
#define LL long long
#define INF (1e9+7)
const int MAXN=1e5+5;
int w[35],v[35],f[1005];
bool flag,b[35][1005];
int main() {
// freopen("Diving_for_Gold.txt","w",stdout);
int t,ww,n;
while (scanf("%d%d\n%d",&t,&ww,&n)==3) {
if (!flag) {
flag=true;
} else puts("");
memset(w,0,sizeof(w));
memset(v,0,sizeof(v));
memset(f,0,sizeof(f));
memset(b,0,sizeof(b));
// printf("%d\n",n);
for (int i=1;i<=n;i++) {
scanf("%d %d",&v[i],&w[i]);
v[i]*=ww*3;
// printf("v:%d ",v[i]);
}
for (int i=1;i<=n;i++) {
for (int j=t;j>=v[i];j--) {
if (f[j-v[i]]+w[i]>f[j]) {
f[j]=f[j-v[i]]+w[i];
// puts("Yes");
b[i][j]=true;
}
}
}
// for (int i=1;i<=t;i++) {
// printf("f[%d]=%d\n",i,f[i]);
// }
int now=t;
std::vector<int> u;
for (int i=n;i>=1;i--) {
if (b[i][now]) {
u.push_back(i);
now-=v[i];
}
}
printf("%d\n%d\n",f[t],u.size());
for (int i=u.size()-1;i>=0;i--) {
printf("%d %d\n",v[u[i]]/3/ww,w[u[i]]);
}
}
return 0;
}
//
//
//
// 210 4
// 3
// 10 5
// 10 1
// 7 2