E. Two Arrays and Sum of Functions(排序不等式)

E. Two Arrays and Sum of Functions

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

本题并不难

我们只需要考虑

每个相乘之后的之对答案的贡献次数

很容易推出贡献的次数为i*(n+1-i)

由于a的位置是不动的那么我们就可以把啊ai改写成ai*i*(n+1-i)

根据排序不等式https://en.wikipedia.org/wiki/Rearrangement_inequality

此时我们就可以对ab根据相反的规则来排序

排序后就可以直接计算出ai的值了

accode

#include<bits/stdc++.h>
using namespace std;
const long long mod=998244353;
long long a[200005];
long long b[200005];
int  cmp(long long a,long long b)
{
    return a>b;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&b[i]);
    }
    for(int i=1;i<=n;i++)
    {
        a[i]=(a[i]*i*(n-i+1));
    }
    sort(a+1,a+1+n);
    sort(b+1,b+1+n,cmp);
    long long ans=0;
    long long tmp=0;
    for(int i=1;i<=n;i++)
    {
        a[i]=a[i]%mod;
        b[i]=b[i]%mod;
        tmp=(a[i]*b[i])%mod;
        ans=(ans+tmp)%mod;
    }
    printf("%lld\n",ans);
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值