Advertisement
apl-mhd

weeding shopping 11450 TLE

Aug 13th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6.  
  7. using namespace std;
  8. /*
  9.     8 6 4
  10. 2 5 10
  11. 4 1 3 3 7
  12. 4 50 14 23 8
  13.     */
  14.  
  15. int main(int argc, char **argv)
  16. {
  17.  
  18.  
  19.  
  20.     int testCase;
  21.     cin>>testCase;
  22.     while(testCase--){
  23.  
  24.     int totalAmount, cloths;
  25.    
  26.     cin>>totalAmount>>cloths;
  27.     vector<int>garment[cloths];
  28.    
  29.     for(int m=0; m<cloths; m++){
  30.         int tklen;
  31.         cin>>tklen;
  32.        
  33.         for(int n=0; n<tklen; n++){
  34.            
  35.             int tk;
  36.             cin>>tk;
  37.             garment[m].push_back(tk);
  38.    
  39.    
  40. }
  41. }
  42.    
  43.     for(int i=0; i<cloths-1; i++){
  44.        
  45.         int secondSize = garment[i+1].size();
  46.        
  47.         for(int j=0; j<garment[i].size(); j++){
  48.                 int x = garment[i][j];
  49.            
  50.             for(int k=0; k<secondSize; k++){
  51.                
  52.                 int y = garment[i+1][k];
  53.                
  54.                 if (x+y < totalAmount & (i!=cloths-2))
  55.                     garment[i+1].push_back(x+y);
  56.                 else if(x+y<= totalAmount)
  57.                     garment[i+1].push_back(x+y);
  58.                
  59.                 }
  60.            
  61.                
  62.             }
  63.            
  64.             garment[i+1].erase(garment[i+1].begin(),garment[i+1].begin()+secondSize);
  65.        
  66.         }
  67.        
  68.        
  69.        /*  
  70. for(int i=0; i<cloths; i++){
  71.        
  72.         //sort(garment[3].begin(), garment[3].end());
  73.  
  74.         for (int j=0; j<garment[i].size(); j++){
  75.            
  76.             cout<<garment[i][j]<<" ";
  77.             }
  78.            
  79.         cout<<endl<<"\n";
  80.        
  81.         }  
  82.        
  83.         */
  84.  
  85.     sort(garment[cloths-1].begin(), garment[cloths-1].end(), greater<int>());
  86.    
  87.         if(garment[cloths-1][0]<=totalAmount){
  88.        
  89.                 cout<<garment[cloths-1][0]<<endl;
  90.         }
  91.         else
  92.             cout<<"no solution"<<endl;
  93.            
  94.            
  95.        
  96.  
  97.        
  98.    
  99. }
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement