AlbertMourato

Untitled

Feb 23rd, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <bits/stdc++.h>
  3. //%lld
  4. typedef long long ll;
  5. using namespace std;
  6. int peso[2005];
  7. int valor[2005];
  8. int matriz[2005][2005];
  9. int main(){
  10.     //freopen("in.txt", "r", stdin);
  11.     //freopen("out.txt", "w", stdout);
  12.     int s, n;
  13.     cin>>s>>n;
  14.     for(int i = 0;i<n;i++){
  15.         cin>>peso[i]>>valor[i];
  16.     }
  17.     for(int i = peso[0];i<=s;i++){
  18.         matriz[0][i] = valor[0];
  19.     }
  20.     for(int i = 1;i<=n;i++){
  21.         for(int j = 1;j<=s;j++){
  22.             if(peso[i]>j){
  23.                 matriz[i][j] = matriz[i-1][j];
  24.             }else{
  25.                 matriz[i][j] = max((matriz[i-1][j-peso[i]]+valor[i]), matriz[i-1][j]);
  26.             }
  27.         }
  28.     }
  29.     int saida = 0;
  30.     for(int i = 0;i<=s;i++){
  31.         if(matriz[i][s]>saida)saida = matriz[i][s];
  32.     }
  33.     cout<<saida<<"\n";
  34. }
Advertisement
Add Comment
Please, Sign In to add comment