We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<cmath>#include<cstdio>#include<vector>#include<iostream>#include<algorithm>usingnamespacestd;boolcmp(pair<longlongint,char>a,pair<longlongint,char>b){returna.first>b.first;}intmain(){longlonginti,j,k,v,t,n,m;cin>>t;for(v=0;v<t;v++){cin>>m>>n;vector<pair<longlongint,char>>vect;for(i=0;i<m-1;i++){cin>>j;vect.push_back(make_pair(j,'h'));}for(i=0;i<n-1;i++){cin>>j;vect.push_back(make_pair(j,'v'));}sort(vect.begin(),vect.end(),cmp);longlongintht_segments=0,vt_segments=0,ans=0;vector<pair<longlongint,char>>::iteratorii;for(ii=vect.begin();ii!=vect.end();ii++){if(ii->second=='h'){if(ht_segments==0)ht_segments+=2;elseht_segments++;if(vt_segments>0)ans+=(ii->first*vt_segments);elseans+=ii->first;}else{if(vt_segments==0)vt_segments+=2;elsevt_segments++;if(ht_segments>0)ans+=(ii->first*ht_segments);elseans+=ii->first;}}cout<<ans%(1000000007)<<"\n";}/* Enter your code here. Read input from STDIN. Print output to STDOUT */return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cutting Boards
You are viewing a single comment's thread. Return to all comments →
Easy Solution in C++ :) Happy Coding :)