Codeforces Round #560 (Div. 3) -> E. Two Arrays and Sum of Functions

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)=liraibi

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) 1lrnf(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(1n2105) — 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(1ai106), where a i a_i ai is the i − i- ith 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(1bj106), where b j b_j bj is the j − j- jth 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) 1lrnf(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的地方及时取模

<此题感谢张总>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值