You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.4 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int T; //T组数据
int n; //每一组数据的个数n
int a[N]; //每个商店的金钱数量
/**
f[i][0] i
f[i][1] i
*/
int f[N][2];
/**
O(n)
f[i][0] i , f[i][1] i
f[i][0] 1. i - 1 2. i - 1
f[i][1] i - 1
*/
//状态机解法
int main() {
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
memset(f, 0, sizeof f);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
//初始化
f[1][0] = 0, f[1][1] = a[1];
//逐个把商店加入
for (int i = 2; i <= n; i++) {
//不偷i号商店,获利取原来前面i-1号商店决策完的最大值
f[i][0] = max(f[i - 1][0], f[i - 1][1]);
//偷i号商店获利取不偷i-1号商店的决策值再加上当前商店的金额
f[i][1] = f[i - 1][0] + a[i];
}
//最终的结果二选一
printf("%d\n", max(f[n][0], f[n][1]));
}
return 0;
}