fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. typedef long long ll;
  4.  
  5. ll M = 998244353ll;
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10. ios_base::sync_with_stdio(false);
  11. cin.tie(NULL); cout.tie(NULL);
  12. ll n; cin>>n;
  13. vector<ll> a(n);
  14. for(ll i = 0; i < n ; i++){
  15. cin>>a[i];
  16. a[i]*=(i+1ll)*(n - i);
  17. }
  18. vector<ll>b(n);
  19. for(int i = 0; i < n;i++) cin>>b[i];
  20. sort(a.rbegin(), a.rend());
  21. sort(b.begin(), b.end());
  22. ll ans = 0;
  23. for(int i = 0;i<n;i++){
  24. ll val = (a[i])%M;
  25. ll v2 = b[i]%M;
  26. val = (val*v2)%M;
  27. ans = (ans + val)%M;
  28. }
  29. cout<<ans<<endl;
  30. }
  31.  
Success #stdin #stdout 0.01s 5288KB
stdin
2
1 3
4 2

stdout
20