Round560div3.E

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 a

and b, both of length n

.

Let's define a function f(l,r)=∑l≤i≤rai⋅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≤nf(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⋅105) — the number of elements in a and b

.

The second line of the input contains n

integers a1,a2,…,an (1≤ai≤106), where ai is the i-th element of a

.

The third line of the input contains n

integers b1,b2,…,bn (1≤bj≤106), where bj is the j-th element of b

.

Output

Print one integer — the minimum possible value of ∑1≤l≤r≤nf(l,r)

after rearranging elements of b, taken modulo 998244353

. 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

 

这个题大意是给你数组a和数组b然后可以对b进行随意的排序

如何使得题目中的表达式结果最小

 

我一开始想得是直接对a和b分别排序

a从小到大 b从大到小

然后a[i]*b[i]都是计算了n次直接输出结果

但是快结束的时候才发现 a[i]其实计算了i*(n-i+1)次(i从1开始)

所以这时候我们先处理一下a[i] a[i]=a[i]*i*(n-i+1) 然后把处理过的a[i]进行排序就可以了

(让b[i]最大乘以a[i]最小)

/*#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;typedef long long ll;
ll a[200000+100];
ll b[200000+100];

ll qian[200000+100];
#define mu 998244353
bool cmp(const int a,const int b){
return a>b;
}
int num[10000];
int main(){
    int n;
    cin>>n;
    for(int i =1;i<=n;++i){
        scanf("%lld",&a[i]);
    }
    for(int i = 1;i<=n;++i){
        scanf("%lld",&b[i]);
    }

    sort(a+1,a+n+1);
    sort(b+1,b+n+1,cmp);
    for(int i =1;i<=n;++i){
        cout<<a[i]<<":";
    }
    cout<<endl;
    for(int i =1;i<=n;++i){
        cout<<b[i]<<":";
    }
    cout<<endl;
    ll sum = 0;

    for(int i =1;i<=n;++i){
        sum+=((((a[i]*b[i])%mu)*n)%mu)%mu;
        //cout<<sum<<endl;
    }
    for(int i =1;i<=n;++i){
        for(int j =i;j<=n;++j){
            for(int k=i;k<=j;++k){
                sum+=a[k]*b[k];
                num[k]++;
                cout<<k<<":";
            }
            cout<<endl;
        }
    }
    for(int i =1;i<=n;++i){
        cout<<num[i]<<endl;
    }
    cout<<sum<<endl;


}
*/

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
#define mu 998244353
ll a[1000000+100];
ll b[1000000+100];
bool cmp(const int a,const int b){
    return a>b;
}
int main(){
    int n;
    cin>>n;
    for(int i =1;i<=n;++i){
        scanf("%lld",&a[i]);
        a[i]=a[i]*(i)*(n-i+1);
    }

    for(int i =1;i<=n;++i){
        scanf("%lld",&b[i]);
    }
    sort(a+1,a+1+n);
    sort(b+1,b+1+n,cmp);

    ll res = 0;
    for(int i =1;i<=n;++i){
        res=(res+(a[i]%mu)*(b[i]%mu))%mu;
    }
    cout<<res<<endl;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值