提交时间:2023-12-16 08:39:43

运行 ID: 116131

#include<iostream> using namespace std; const int N = 1e2 + 10; int f1[N], f2[N], a[N], n, ans = 0; int main(){ cin >> n; for(int i = 0; i < n; i++){ cin >> a[i]; } if(n == 2){ cout << max(a[0], a[1]); return 0; } if(n == 1){ cout << a[0]; return 0; } for(int i = 3; i < n; i++){ f1[i] = max(f1[i], f1[i-2] + a[i-2]); if(i == 3) continue; f1[i] = max(f1[i], f1[i-3] + a[i-3]); } ans = max(f1[n-1] + a[n-1], f1[n-2] + a[n-2]); for(int i = 2; i < n-1; i++){ f2[i] = max(f2[i], f2[i-2] + a[i-2]); if(i == 2) continue; f2[i] = max(f2[i], f2[i-3] + a[i-3]); } ans = max(ans, max(f2[n-2] + a[n-2], f2[n-3] + a[n-3])); cout << ans; return 0; }