第一道没看题解的绿题!!!(老师帮我看了半天)
优美的代码:
#include<iostream>
#include<cstring>
using namespace std;
const int N = 110,M=1e4+10;
const int INF = -0x3f3f3f;
int T,n,m;
int p[N][N];
int dp[N][M];
int f[N];//f[i]为第i天卖掉所有东西后剩余的钱
int rest;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>T>>n>>m;
for(int i =1;i<=T;i++){
for(int j = 1;j<=n;j++){
cin>>p[i][j];
}
}
f[1] = m;
for(int t = 2;t<=T;t++){//dp:t-1到t这两天之间买和卖的过程
memset(dp,INF,sizeof(dp));
dp[0][0] = 0;
for(int i =1;i<=n;i++){
int w = p[t-1][i];//空间是t-1天的东西的价格
int v = p[t][i];//收益是t天的东西的价格
for(int j = 0;j<=f[t-1];j++){//注意:这里的限制是t-1天的钱
dp[i][j] = dp[i-1][j];
if(j>=w){
dp[i][j] = max(dp[i][j],dp[i][j-w]+v);
}
}
}
for(int i=0;i<=f[t-1];++i){
f[t] = max(f[t], dp[n][i] + f[t-1] - i);
}
}
cout<<f[T];
return 0;
}