➤ Problem Link : CHOCOLA
👉 Hint : Think about sorting the arrays
✅ C++ Solution :
#include<bits/stdc++.h>
using namespace std;
int dp[1001][1001];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--)
{
int n,m;
cin>>m>>n;
int arrm[m-1];
int arrn[n-1];
for(int i=0;i<m-1;i++)
cin>>arrm[i];
for(int i=0;i<n-1;i++)
cin>>arrn[i];
sort(arrm,arrm+m-1,greater<int>());
sort(arrn,arrn+n-1,greater<int>());
int h=1,v=1;
int i=0,j=0;
int ans=0;
while(i<m-1 && j<n-1)
{
if(arrm[i]>=arrn[j])
{
ans+=arrm[i]*h;
v++;
i++;
}
else
{
ans+=arrn[j]*v;
h++;
j++;
}
}
while(i<m-1)
{
ans+=arrm[i]*h;
i++;
}
while(j<n-1)
{
ans+=arrn[j]*v;
j++;
}
cout<<ans<<endl;
}
}
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!
