This problem too much water,so there is code:
#include <iostream> #include <cstring> using namespace std; const int MAXN=105; int w[MAXN][MAXN]; int dp[MAXN][MAXN][MAXN]; int n,m,kk; int max(int a,int b){ if(a<b)return b; return a; } int main(){ while(cin>>n>>m>>kk){ //cout<<n<<" "<<m<<" "<<kk<<endl; memset(dp,0,sizeof dp); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ cin>>w[i][j]; } } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ for(int k=kk;k>=0;k--){ dp[i][j][k]=max(dp[i][j][k],max(dp[i-1][j][k],dp[i][j-1][k])); if(k>=w[i][j]) { dp[i][j][k]=max(dp[i][j][k],dp[i-1][j][k-w[i][j]]+w[i][j]); dp[i][j][k]=max(dp[i][j][k],dp[i][j-1][k-w[i][j]]+w[i][j]); } } } } cout<<dp[n][m][kk]<<endl; } }