此题还有一个进阶版:1025. 开餐馆
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int n;
int cash[N], f[N];
int main()
{
int T;
scanf("%d", &T);
while (T -- )
{
cin >> n;
for (int i = 0; i < n; i ++ ) scanf("%d", &cash[i]);
f[0] = 0, f[1] = cash[0]; //状态初始化
for (int i = 2; i <= n; i ++ )
f[i] = max(f[i - 1], f[i - 2] + cash[i - 1]);
//当前最大值 = max(当前家不抢, 当前家抢 + 上上家最大值)
printf("%d\n", f[n]);
}
return 0;
}