题解 | #不相邻取数#
跳跃游戏(二)
http://www.nowcoder.com/practice/58e31b785f4b4ced9695dd4fcd60c1ce
#include<iostream>
using namespace std;
int dp[100000];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
bool ans = true;
cin >> n;
int* num = new int[n];
for (int i = 0; i < n; ++i) {
cin >> num[i];
dp[i] = 0;
}
dp[0] = num[0];
for (int i = 0; i < n; ++i) {
for (int j = i + 1; dp[i] && j <= i + num[i] && j < n; ++j) {
dp[j] = max(dp[j], dp[i] + num[j]);
}
}
if (dp[n - 1] == 0)
cout << -1;
else
cout << dp[n - 1];
}