题解 | #不相邻取数#
不相邻取数
https://www.nowcoder.com/practice/a2be806a0e5747a088670f5dc62cfa1e
#include <iostream> using namespace std; const int N = 2 * 100010; int a[N], f[N]; int main() { int n; cin >> n; for(int i = 1; i <= n; i ++) cin >> a[i]; f[1] = a[1]; for(int i = 2; i <= n; i ++){ f[i] = max(f[i - 2] + a[i], f[i - 1]); } cout << f[n] << endl; }