Algorithm/ct/l2/3-动态规划-不相邻取数.py

14 lines
216 B
Python
Raw Normal View History

2022-10-28 02:37:25 +08:00
import io
import sys
sys.stdin = io.StringIO('''4
2 6 4 1''') # 7
n = int(input())
a = list(map(int, input().split()))
dp0, dp1 = 0, a[0]
for i in range(1, n):
dp0, dp1 = dp1, max(dp1, dp0 + a[i])
print(dp1)