POJ - 2228 Naptime(环形结构DP)
#include <iostream>
#include <cstring>
using namespace std;
const int N = 4000, INF = 0x3f3f3f3f;
int n,m,w[N],f[2][N][2];
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) cin >> w[i];
memset(f, -0x3f, sizeof f);
f[1][0][0] = f[1][1][1] = 0;// 第n小时不在睡觉
solve();
int res = f[n & 1][m][0];
memset(f, -0x3f, sizeof f);
f[1][0][0] = 0,f[1][1][1] = w[1];// 第n小时在睡觉
for (int i = 2; i <= n; i ++ )
for (int j = 0; j <= m; j ++ )
{
f[i & 1][j][0] = max(f[i - 1 & 1][j][0], f[i - 1 & 1][j][1]);
f[i & 1][j][1] = -INF;
if (j) f[i & 1][j][1] = max(f[i - 1 & 1][j - 1][0], f[i - 1 & 1][j - 1][1] + w[i]);
}
res = max(res, f[n & 1][m][1]);
cout << res << endl;
return 0;
}