设dp[i][j],表示插第i朵花插到第j个花瓶时,最大值多少
若不插在花瓶j中则,dp[i][j]=dp[i][j-1]
若插,则dp[i][j]=max{dp[i-1][j-1]+a[i][j]};
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=110; int a[maxn][maxn]; int dp[maxn][maxn]; int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ for (int j=1;j<=m;j++){ scanf("%d",&a[i][j]); } } memset(dp,-127,sizeof(dp)); for (int i=0;i<=m;i++) dp[0][i]=0; for (int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ dp[i][j]=max(dp[i-1][j-1]+a[i][j],dp[i][j-1]); } } printf("%d ",dp[n][m]); return 0; }