Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- int n;
- int dp[1010];
- vector<int>v;
- vector<int>v1;
- pair<int, int> backtrack[1010];
- int rec(int i=0){
- if(i==n){
- backtrack[i] = make_pair(-1, -1);
- return 0;
- }
- if(dp[i]!=-1){
- return(dp[i]);
- }
- int result=2e9;
- int sam = rec(i+1)+v[i];
- int spoen = 2e9;
- if(i+1<n){
- spoen = rec(i+2)+v1[i];
- }
- result = min(sam, spoen);
- if(sam <= spoen) {
- backtrack[i] = make_pair(i + 1, -1);
- }
- else {
- backtrack[i] = make_pair(i + 2, 1);
- }
- dp[i]=result;
- return(result);
- }
- int main()
- {
- cin>>n;
- for(int i=0; i<n; i++){
- int a;
- cin>>a;
- v.push_back(a);
- dp[i]=-1;
- }
- for(int j=0; j<n-1; j++){
- int b;
- cin>>b;
- v1.push_back(b);
- }
- rec(0);
- int at =0 ;
- while(at < n) {
- if(backtrack[at].second == 1) {
- cout << backtrack[at].first - 1 << " " << backtrack[at].first << endl;
- }
- at = backtrack[at].first;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement