题解 | #牛牛吃草#
牛牛吃草
https://www.nowcoder.com/practice/f05254f070944ff792c0dfefabd94fec
#include<iostream> #include<vector> using namespace std; class Solution{ public: unsigned long eatSumGrass(vector<int> &W, vector<int> &A){ vector<int> res(W.size(),0); int MaxRes = 0; for(int i=W.size()-1; i>=0; i--){ int tmp = 0; int j = i; while(j + A[i] < W.size()){ j += A[i]; if(res[j]> tmp){ tmp = res[j]; } } res[i] = W[i] + tmp; if(res[i]> MaxRes) MaxRes = res[i]; } return MaxRes; } }; int main(){ int n; // 草地数量 vector<int> W; // 草的数量 vector<int> A; // 前进步数 cin>>n; W.resize(n); A.resize(n); for(int i=0; i<n; i++){ cin>>W[i]; } for(int i=0; i<n; i++){ cin>>A[i]; } Solution s; unsigned long res = s.eatSumGrass(W, A); cout<<res; return 0; }