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.

32 lines
778 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N]; //原始数组
int f[N]; //递推结果数组
/*
i = max(i-1i-2 +i)
(Dp 2ms)
O(n)
*/
int dfs(int u) {
if (u <= 0) return 0;//注意一下这个边界
if (f[u]) return f[u];//记忆化搜索的灵魂
return f[u] = max(dfs(u - 1), dfs(u - 2) + a[u]);
}
int main() {
int T, n;
scanf("%d", &T);
while (T--) {
scanf("%d", &n);
//每次重置一下结果数组
memset(f, 0, sizeof f);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
printf("%d\n", dfs(n));
}
return 0;
}