标签:dp
3 2 1 1 1 2 3 4 2 1 2 1 2 3 5 0
5 10
Statistic | Submit | Discuss | Note
dp[i][j]表示把前i个数分成j份,得到的和的最大值,则 dp[i][j] = max(dp[i - 1][j], dp[i - seg[j][j - 1] + sum);
#include <map> #include <set> #include <list> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 1010; const int M = 22; int sum[N]; int dp[N][M]; int arr[N], seg[M]; int main() { int n, m; while(~scanf("%d", &n), n) { sum[0] = 0; scanf("%d", &m); for (int i = 1; i <= m; ++i) { scanf("%d", &seg[i]); } for (int i = 1; i <= n; ++i) { scanf("%d", &arr[i]); sum[i] = sum[i - 1] + arr[i]; } memset (dp, 0, sizeof(dp)); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { dp[i][j] = max(dp[i][j], dp[i - 1][j]); if (i > seg[j]) { dp[i][j] = max(dp[i][j], dp[i - seg[j]][j - 1] + sum[i] - sum[i - seg[j]]); } } } printf("%d\n", dp[n][m]); } return 0; }
hdu1244——Max Sum Plus Plus Plus
标签:dp
原文地址:http://blog.csdn.net/guard_mine/article/details/41846955