fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef vector<int> vi;
  6. typedef vector<ll> vl;
  7. typedef vector<pair<ll,ll> > vll;
  8. typedef pair<int, int> ii;
  9. typedef pair<ll,ll> pll;
  10. typedef vector<ii> vii;
  11. typedef map<int,int> mii;
  12. typedef map<ll,ll> mll;
  13. typedef map<char,int> mci;
  14. typedef map<char,ll> mcl;
  15.  
  16. #define mod 1000000007
  17. #define pb push_back
  18. #define mp make_pair
  19. #define ff first
  20. #define ss second
  21.  
  22. int main()
  23. {
  24. ios_base::sync_with_stdio(false);
  25. cin.tie(NULL);cout.tie(NULL);
  26.  
  27. ll t;
  28. cin>>t;
  29.  
  30. for(int c=1;c<=t;c++)
  31. {
  32. ll n,k,p;
  33. cin>>n>>k>>p;
  34.  
  35. ll mat[n][k];
  36. for(int i=0;i<n;i++)
  37. {
  38. for(int j=0;j<k;j++)
  39. {
  40. cin>>mat[i][j];
  41. }
  42. }
  43.  
  44. vector<stack<int> > v1;
  45. for(int i=0;i<n;i++)
  46. {
  47. stack<int> st;
  48. for(int j=k-1;j>=0;j--)
  49. st.push(mat[i][j]);
  50.  
  51. v1.push_back(st);
  52. }
  53.  
  54. ll ans=0;
  55. int max_index;
  56. int max_sum;
  57.  
  58. while(p--)
  59. {
  60. max_index=-1;
  61. max_sum=-1;
  62. for(int i=0;i<n;i++)
  63. {
  64. int sum=0;
  65. stack<int> kt=v1[i];
  66. int x=p+1;
  67.  
  68. //trying to take the sum of top p elements of the ith stack. Will consider that stack whose sum is max which means i can potentially get greater elements
  69.  
  70. while(!kt.empty()&& x--)
  71. {
  72. sum+=kt.top();
  73. kt.pop();
  74. }
  75. if(sum>max_sum)
  76. {
  77. max_sum=sum;
  78. max_index=i;
  79. }
  80. }
  81. ans+=v1[max_index].top();
  82. //cout<<ans<<" "<<max_index<<" "<<endl;
  83. v1[max_index].pop();
  84. }
  85.  
  86. cout<<"Case #"<<c<<": "<<ans<<endl;
  87. }
  88.  
  89.  
  90. return 0;
  91. }
Success #stdin #stdout 0s 4460KB
stdin
2
2 4 5
10 10 100 30
80 50 10 50
3 2 3
80 80
15 50
20 10
stdout
Case #1: 250
Case #2: 180