1165E. Two Arrays and Sum of Functions - Codeforces Solution C++

  Problem Link : 1165E. Two Arrays and Sum of Functions 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

#define ll long long int
#define M 998244353 
int main()
{
	int n;
	cin>>n;
	ll a[n],b[n],c[n];
	multimap<double,int> mp;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		mp.insert(make_pair(log(i+1)+log(n-i)+log(a[i]),i));
	}
	for(int i=0;i<n;i++)
		cin>>b[i];

	sort(b,b+n,greater<int>());
	int i=0;
	ll ans=0;
	for(auto it = mp.begin();it!=mp.end();it++)
	{
		c[(*it).second]=b[i];
		ll ind = (*it).second;
		ans=(ans+((((ind+1)%M*(n-ind)%M)%M*(a[ind])%M)%M * b[i]%M)%M)%M;
		i++;
	}

//	for(int i=0;i<n;i++)
//		cout<<c[i]<<" ";
    cout<<ans;	
}

 

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!!