题解 | #跳跃游戏(二)#
跳跃游戏(二)
https://www.nowcoder.com/practice/58e31b785f4b4ced9695dd4fcd60c1ce
#include <iostream> #include<cstring> using namespace std; const int N = 100010; int a[N], f[N]; int jump(int n){ if(n == 0) return -1; if(n == 1) return a[0]; f[0] = a[0]; for(int i = 0; i < n; i ++){ if(f[i] != -1){ int maxReach = min(n - 1, i + a[i]); for(int j = i + 1; j <= maxReach; j ++){ f[j] = max(f[j], f[i] + a[j]); } } } return f[n - 1]; } int main() { int n; cin >> n; memset(f, -1, sizeof f); for (int i = 0; i < n; i ++) { cin >> a[i]; } cout << jump(n) << endl; return 0; }