AlbertMourato

Untitled

Feb 23rd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 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. //Knapsack
  7. int peso[1000];
  8. int valor[1000];
  9. int matriz[1000][1000];
  10. int main(){
  11.     //freopen("in.txt", "r", stdin);
  12.     //freopen("out.txt", "w", stdout);
  13.     int s, n;
  14.     while(scanf("%d", &n)==1){
  15.         if(n==0)break;
  16.         else{
  17.             memset(matriz, 0, sizeof(matriz));
  18.             memset(valor, 0, sizeof(valor));
  19.             memset(peso, 0, sizeof(peso));
  20.             scanf("%d", &s);
  21.             for(int i = 0;i<n;i++){
  22.                 scanf("%d%d", &valor[i], &peso[i]);
  23.             }
  24.             for(int i = peso[0];i<=s;i++){
  25.                 matriz[0][i] = valor[0];
  26.             }
  27.             for(int i = 1;i<=n;i++){
  28.                 for(int j = 1;j<=s;j++){
  29.                     if(peso[i]>j){
  30.                         matriz[i][j] = matriz[i-1][j];
  31.                     }else{
  32.                         matriz[i][j] = max((valor[i]+matriz[i-1][j-peso[i]]), matriz[i-1][j]);
  33.                     }
  34.                 }
  35.             }
  36.             int saida = 0;
  37.             for(int i = 0;i<=s;i++){
  38.                 if(matriz[i][s]>saida)saida = matriz[i][s];
  39.             }
  40.             printf("%d min.\n", saida);
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment