dp,参考了http://www.cppblog.com/hanfei19910905/archive/2012/06/30/180831.html;
定义两组状态 L1[i] 表示在 0-i 内的最大得分,L2[i] 表示从 i 出发回到 i 的最大得分,不难得到状态转移方程,最后枚举 i 组合求出最大值;
# include <cstdio> # include <algorithm> # define N 100005 using namespace std; typedef long long int LL; int n, num[N]; LL L1[N], L2[N], R1[N], R2[N], ans; void init(void) { int i; scanf("%d", &n); for (i = 1; i < n; ++i) scanf("%d", &num[i]); } void solve(void) { int i; L1[0] = L2[0] = 0; for (i = 1; i < n; ++i) { L2[i] = (num[i]>1 ? L2[i-1]+num[i]/2*2:0); if (num[i] & 0x1) L1[i] = L1[i-1] + num[i]; else L1[i] = max(L2[i], L1[i-1]+num[i]-1); } R2[n-1] = R1[n-1] = 0; for (i = n-2; i >= 0; --i) { R2[i] = (num[i+1]>1 ? R2[i+1]+num[i+1]/2*2:0); if (num[i+1] & 0x1) R1[i] = R1[i+1] + num[i+1]; else R1[i] = max(R2[i], R1[i+1]+num[i+1]-1); } ans = 0; for (i = 0; i < n; ++i) { ans = max(ans, max(max(L1[i], R1[i]), max(L1[i]+R2[i], L2[i]+R1[i]))); } printf("%I64d\n", ans); } int main() { //freopen("in.txt", "r", stdin); init(); solve(); return 0; }