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(l,r)=∑l≤i≤r ai⋅bi.
Your task is to reorder the elements (choose an arbitrary order of elements) of the array b to minimize the value of ∑1≤l≤r≤n f(l,r). Since the answer can be very large, you have to print it modulo 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⋅10^5) — the number of elements in a and b.
The second line of the input contains n integers a1,a2,…,an (1≤ai≤10^6), where ai is the i-th element of a.
The third line of the input contains n integers b1,b2,…,bn (1≤bj≤10^6), where bj is the j-th element of b.
Output
Print one integer — the minimum possible value of ∑1≤l≤r≤n f(l,r) after rearranging elements of b, taken modulo 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
解题思路:
可以自己找一下规律
c[i] = (i+1)(n-i) i∈[0,n-1]
按a[i]*c[i]升序排序,
给b降序排序。
ans = Σ a[i]*b[i]
偷懒起见,用python
代码:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n):
a[i] = a[i]*(i+1)*(n-i)
a.sort()
b.sort(reverse=True)
res = 0
for i in range(n):
res += a[i]*b[i]
print(res % 998244353)