E. Two Arrays and Sum of Functions
You are given two arrays a and b, both of length n.
Let’s define a function f ( x , y ) = ∑ l ≤ i ≤ r a i b i f(x,y)=\displaystyle\sum_{l≤i≤r}a_i b_i f(x,y)=l≤i≤r∑aibi
Your task is to reorder the elements (choose an arbitrary order of elements) of the array b b b to minimize the value of ∑ 1 ≤ l ≤ r ≤ n f ( l , r ) \displaystyle\sum_{1≤l≤r≤n}f(l,r) 1≤l≤r≤n∑f(l,r). Since the answer can be very large, you have to print it modulo 998244353 998244353 998244353. Note that you should minimize the answer but not its remainder.
Input
The first line of the input contains one integer
n
(
1
≤
n
≤
2
⋅
1
0
5
)
n (1≤n≤2⋅10^5)
n(1≤n≤2⋅105) — the number of elements in
a
a
a and
b
b
b.
The second line of the input contains n integers a 1 , a 2 , … , a n ( 1 ≤ a i ≤ 1 0 6 ) a_1,a_2,…,a_n (1≤a_i≤10^6) a1,a2,…,an(1≤ai≤106), where a i a_i ai is the i − i- i−th element of a.
The third line of the input contains n integers b 1 , b 2 , … , b n ( 1 ≤ b j ≤ 1 0 6 ) b_1,b_2,…,b_n (1≤b_j≤10^6) b1,b2,…,bn(1≤bj≤106), where b j b_j bj is the j − j- j−th element of b b b.
Output
Print one integer — the minimum possible value of
∑
1
≤
l
≤
r
≤
n
f
(
l
,
r
)
\displaystyle∑_{1≤l≤r≤n}f(l,r)
1≤l≤r≤n∑f(l,r) after rearranging elements of
b
b
b, taken modulo
998244353
998244353
998244353. Note that you should minimize the answer but not its remainder.
Examples
input
5
1 8 7 2 4
9 7 2 9 3
output
646
input
1
1000000
1000000
output
757402647
input
2
1 3
4 2
output
20
AC:
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
#define ll long long
#define db double
#define FOPEN freopen("C:\\Users\\14016\\Desktop\\cad.txt","r",stdin)
unsigned long long a[200005],b[200005];
unsigned long long ans;
int main()
{
long long n;
// FOPEN;
while(cin>>n)
{
for(ll i=1; i<=n; i++)
{
cin>>a[i];
a[i]=a[i]*i*(n-i+1);
}
for(ll i=1; i<=n; i++)
{
cin>>b[i];
}
sort(a+1,a+n+1);
sort(b+1,b+n+1,greater<int>());
ans=0;
for(ll i=1; i<=n; i++)
{
ans=(ans+a[i]%998244353*b[i])%998244353;
}
cout<<ans<<endl;
}
}
解题:
尤其注意!!注意数组要开在main函数外面!并且在每一个可能爆long long的地方及时取模
<此题感谢张总>