Beingamanforever

Two Arrays and Sum of Functions, Contribution Technique

Jan 19th, 2025
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4. #define all(x) (x).begin(), (x).end()
  5. typedef vector<int> vi;
  6. typedef vector<vi> vvi;
  7. typedef vector<pair<int, int>> vpi;
  8. typedef pair<int, int> pi;
  9. #define f first
  10. #define s second
  11. #define pb push_back
  12. #define endl "\n"
  13. #define yes cout << "YES" << endl
  14. #define no cout << "NO" << endl
  15. #define init(x, a) memset(x, a, sizeof(x))
  16. const int mod1 = 1e9 + 7, mod2 = 998244353, INF = 2e18, N = 2e5 + 5, L = 19;
  17. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  18. // -----------------------------------------------------------------------------
  19.  
  20. void solve()
  21. {
  22.     int n;
  23.     cin >> n;
  24.     vi b(n);
  25.     vpi a(n);
  26.     for (int i = 0; i < n; i++)
  27.     {
  28.         int val;
  29.         cin >> val;
  30.         // important to make it mod
  31.         a[i] = {val * 1LL * (n - i) * (i + 1), i}; // contribution
  32.     }
  33.     for (int i = 0; i < n; i++)
  34.     {
  35.         cin >> b[i];
  36.     }
  37.     sort(all(a), greater<>());
  38.     sort(all(b));
  39.     int sum = 0;
  40.     for (int i = 0; i < n; i++)
  41.     {
  42.         sum = (sum + ((a[i].f % mod2) * b[i]) % mod2) % mod2;
  43.     }
  44.     cout << sum << endl;
  45.     return;
  46. }
  47.  
  48. signed main()
  49. {
  50.     // __START__;
  51.     ios_base::sync_with_stdio(false);
  52.     cin.tie(NULL);
  53.     cout.tie(NULL);
  54.     int t = 1;
  55.     // cin >> t;
  56.     while (t--)
  57.     {
  58.         solve();
  59.     }
  60.     // __END__;
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment