VasilM

U2

Jan 17th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #define MAXN 100
  4. using namespace std;
  5.  
  6. int t[MAXN] = { 0, 5, 2, 7, 8, 3, 9, 4, 2 };
  7. int s[MAXN] = { 0, 6, 3, 10, 12, 8, 8, 6 }, M[MAXN], N = 8;
  8.  
  9. int opt()
  10. {   M[1]=t[1];
  11.     M[2]=t[1]+t[2];
  12.     if(M[2]>s[1]) M[2]=s[1];
  13.     for(int i=3;i<=N;i++)
  14.     {  M[i]=t[i]+M[i-1];
  15.        if(M[i]>s[i-1]+M[i-2])
  16.           M[i]=s[i-1]+M[i-2];
  17.     }
  18.     return M[N];
  19. }
  20.  
  21. void restore(int i)
  22. { if(i==1) {printf("(1) ");return;}
  23.   if(i==2)
  24.   {  if(M[2]==t[1]+t[2])
  25.      { printf("1 2 ");return;}
  26.      else { printf("(1 2) " );return;};
  27.   }
  28.   if(M[i]==t[i]+M[i-1])
  29.   { restore(i-1); printf("(%d) ",i);}
  30.   else
  31.   {restore(i-2); printf("(%d %d) ",i-1,i);}
  32. }
  33.  
  34. int main(){
  35.     cout << opt() << endl;
  36.     restore(N);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment