Sanath12345

Untitled

Jul 28th, 2021 (edited)
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. struct tri{
  4. int val;
  5. pair<int,int> p,q;
  6. };
  7. bool comp(tri a,tri b){
  8. return a.val>b.val;
  9. }
  10. int main(){
  11. boost
  12. int n,k;
  13. cin>>n>>k;
  14. int m[n+5][4];
  15. memset(m,0,sizeof(m));
  16. vector<tri> pos;
  17. for(int i=1;i<=n;i++){
  18. for(int j=1;j<=3;j++){
  19. cin>>m[i][j];
  20. }
  21. }
  22. for(int i=1;i<=n;i++){
  23. for(int j=1;j<=3;j++){
  24. if(i>1) pos.push_back({m[i][j]+m[i-1][j],{i,j},{i-1,j}});
  25. if(j>1) pos.push_back({m[i][j]+m[i][j-1],{i,j},{i,j-1}});
  26. }
  27. }
  28. sort(pos.begin(),pos.end(),comp);
  29. int vis[n+5][4];
  30. memset(vis,0,sizeof(vis));
  31. int l=pos.size();
  32. long long int ans=0;
  33. for(int i=0;i<l and k>0;i++){
  34. pair<int,int> x=pos[i].p;
  35. pair<int,int> y=pos[i].q;
  36. if(vis[x.first][x.second]==0 and vis[y.first][y.second]==0){
  37. ans+=pos[i].val;
  38. vis[x.first][x.second]=1;
  39. vis[y.first][y.second]=1;
  40. k--;
  41. }
  42. }
  43. cout<<ans<<endl;
  44. }
Add Comment
Please, Sign In to add comment