giabinh

TrangTri

Jul 23rd, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int s, k, n, sum, x[25], a[25][25];
  6. bool ok;
  7.  
  8. void Solve(int t) {
  9.     for(int i=1;i<=n;i++)
  10.         if(a[i][t]>=x[t-1]) {
  11.             if(a[i][t]<=(s-sum)/(k-t+1)) {
  12.                 x[t]=a[i][t];
  13.                 sum+=x[t];
  14.                 if(t==k) {
  15.                     if(sum==s) ok=true;
  16.                 } else Solve(t+1);
  17.                 sum-=x[t];
  18.             }
  19.             if(ok) return;
  20.         }
  21. }
  22.  
  23. int main()
  24. {
  25.     #ifndef ONLINE_JUDGE
  26.         freopen("trangtri.inp", "r", stdin);
  27.     #endif // ONLINE_JUDGE
  28.  
  29.     scanf("%d%d%d", &s, &k, &n);
  30.     for(int i=1;i<=n;i++)
  31.         for (int j=1;j<=k;j++) scanf("%d", &a[i][j]);
  32.     ok=false;
  33.  
  34.     Solve(1);
  35.  
  36.     if(ok) {
  37.         printf("YES\n");
  38.         for (int i=1;i<=k;i++) printf("%d ",x[i]);
  39.     } else printf("NO");
  40.  
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment