fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define sd(a) scanf("%d", &a)
  5. #define slld(a) scanf("%lld", &a)
  6. #define fl(i, a, b) for(int i=a; i<b; i++)
  7. #define fle(i, a, b) for(int i=a; i<=b; i++)
  8. #define ll unsigned long long
  9. #define wl(q) while(q--)
  10. #define MAX 1000000
  11. #define pb push_back
  12. #define mp make_pair
  13. #define fi first
  14. #define se second
  15. #define mod 998244353
  16.  
  17. int main() {
  18. ll n;
  19. slld(n);
  20. ll a[n], b[n], xx[n], yy[n];
  21. fl(i, 0, n) slld(a[i]), xx[i] = a[i];
  22. fl(i, 0, n) slld(b[i]), yy[i] = b[i];
  23. vector<pair<ll, int> > v1;
  24. fl(i, 0, n)
  25. v1.pb(mp(a[i], i));
  26. sort(v1.begin(), v1.end());
  27. vector<pair<ll, int> > v2;
  28. fl(i, 0, n)
  29. v2.pb(mp(b[i], i));
  30. sort(v2.begin(), v2.end());
  31. map<ll, pair<vector<ll>, ll> > m2;
  32. fl(i, 0, n) {
  33. m2[v1[i].fi].fi.pb(v2[n-i-1].fi);
  34. m2[v1[i].fi].se = 0;
  35. }
  36. fl(i, 0, n) {
  37. yy[i] = m2[xx[i]].fi[m2[xx[i]].se];
  38. m2[xx[i]].se++;
  39. }
  40. fl(i, 0, n) {
  41. a[i] = xx[i];
  42. b[i] = yy[i];
  43. }
  44. ll dp[n] = {0};
  45. dp[0] = (a[0]*b[0])%mod;
  46.  
  47. ll curr = dp[0], cnt = 2;
  48. fl(i, 1, n) {
  49. dp[i] = ((dp[i-1] + curr)%mod + (((cnt*a[i])%mod)*b[i])%mod)%mod;
  50.  
  51. curr = (curr + (((((cnt*a[i])%mod)*b[i])%mod)))%mod;
  52. cnt = (cnt + 1)%mod;
  53. }
  54. printf("%lld", dp[n-1]);
  55. return 0;
  56. }
Runtime error #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty