CF-1165E 思维

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值