题目链接:https://codeforces.com/contest/1165/problem/E

题意:给你两个长度为nn的数组a,ba,b, 现在定义f(l,r)=ai×bi,lrf(l, r) = \sum {{a_i} \times {b_i }}, l \leq r , 现在 可以任意调换bb数组数字的位置 求minmin $\sum {f(l, r)} $

题解: 考虑第ii个点的贡献。 很显然应该是i×(ni+1)×aibii\times{(n-i+1)} \times {a_i b_i}
把前面aia_i的部分看成整体,那么现在就变成已知两个数组求minmin $\sum {f(l, r)} 。 想到排序不等式 反序和\leq $乱序和 \leq 正序和。 所以直接求反序和即可

#include <bits/stdc++.h>
#define pa pair<ll, ll>
#define mp make_pair
#define lowbit(x) ((x)&(-x))
#define mem(i, a) memset(i, a, sizeof(i))
#define sqr(x) ((x)*(x))
#define ls (k << 1)
#define rs (k << 1 | 1)
using namespace std;
typedef long long ll;
template <typename T>
inline void read(T &X) {
    X = 0; char ch = 0; T op = 1;
    for(; ch > '9' || ch < '0'; ch = getchar())
        if(ch == '-') op = -1;
    for(; ch >= '0' && ch <= '9'; ch = getchar())
        X = (X << 3) + (X << 1) + ch - 48;
    X *= op;
}

const ll INF = 0x3f3f3f3f;
const ll N = 2e5 + 5;
const ll p = 998244353;
ll n;
ll a[N],b[N];
vector<ll> v;
int main() {
#ifdef INCTRY
    freopen("input.txt", "rt", stdin);
#endif
    read(n);
    for(ll i = 1; i <= n; i++) {
        read(a[i]); a[i] *= (i * (n - i + 1LL));
    }
    sort(a + 1, a + n + 1);

    for(ll i = 1; i <= n; i++) read(b[i]);
    sort(b + 1, b + n + 1);
    ll ans = 0;
    for(ll i = 1; i <= n; i++) {
        ans = b[i] % p * (a[n - i + 1] % p) + ans % p;
        ans %= p;
    }
    cout << ans % p;

#ifdef INCTRY
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}