链接:https://codeforces.com/contest/1165/problem/E
You are given two arrays aa and bb, both of length nn.
Let's define a function f(l,r)=∑l≤i≤rai⋅bif(l,r)=∑l≤i≤rai⋅bi.
Your task is to reorder the elements (choose an arbitrary order of elements) of the array bb to minimize the value of ∑1≤l≤r≤nf(l,r)∑1≤l≤r≤nf(l,r). Since the answer can be very large, you have to print it modulo 998244353998244353. Note that you should minimize the answer but not its remainder.
Input
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in aa and bb.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106), where aiai is the ii-th element of aa.
The third line of the input contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bj≤1061≤bj≤106), where bjbj is the jj-th element of bb.
Output
Print one integer — the minimum possible value of ∑1≤l≤r≤nf(l,r)∑1≤l≤r≤nf(l,r) after rearranging elements of bb, taken modulo 998244353998244353. Note that you should minimize the answer but not its remainder.
Examples
input
Copy
5 1 8 7 2 4 9 7 2 9 3
output
Copy
646
input
Copy
1 1000000 1000000
output
Copy
757402647
input
Copy
2 1 3 4 2
output
Copy
20
代码:
#include<bits/stdc++.h>
using namespace std;
long long n,x,a[10000001],b[10000001],s=0,mod=998244353;
int main()
{
cin>>n;
s=0;
for(int i=1;i<=n;i++)
{
cin>>x;
a[i]=(x*i*(n-i+1));
}
for(int i=1;i<=n;i++)
{
cin>>b[i];
}
sort(a+1,a+1+n);
sort(b+1,b+1+n);
for(int i=1;i<=n;i++)
{
s=(s+(a[i]%mod*b[n-i+1])%mod)%mod;
}
cout<<s;
}