Beingamanforever

DP+Suffix/Prefix Array, O(n)

Dec 10th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. /**
  2.  *    author:  compounding
  3.  *    created: 2024-12-10 14:46:52
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define yes cout << "YES" << endl
  21. #define no cout << "NO" << endl
  22. #define endl "\n"
  23. const int mod = 1000000007;
  24. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  25. const int N = (int)1e5 + 5;
  26. int a[N], maxleft[N], maxright[N], minleft[N], minright[N];
  27. void solve()
  28. {
  29.     int n, p, q, r;
  30.     cin >> n >> p >> q >> r;
  31.     for (int i = 1; i <= n; i++)
  32.     {
  33.         cin >> a[i];
  34.     }
  35.     minleft[1] = a[1], maxleft[1] = a[1];
  36.     for (int i = 2; i <= n; i++)
  37.     {
  38.         minleft[i] = min(minleft[i - 1], a[i]);
  39.         maxleft[i] = max(maxleft[i - 1], a[i]);
  40.     }
  41.     minright[n] = a[n], maxright[n] = a[n];
  42.     for (int i = n - 1; i >= 1; i--)
  43.     {
  44.         minright[i] = min(minright[i + 1], a[i]);
  45.         maxright[i] = max(maxright[i + 1], a[i]);
  46.     }
  47.     int ans = LLONG_MIN;
  48.     for (int i = 1; i <= n; i++)
  49.     {
  50.         int leftval = (p < 0) ? 1LL * minleft[i] * p : 1LL * maxleft[i] * p;
  51.         int rightval = (r < 0) ? 1LL * minright[i] * r : 1LL * maxright[i] * r;
  52.         ans = max(ans, leftval + 1LL * a[i] * q + rightval);
  53.     }
  54.     cout << ans << endl;
  55.     return;
  56. }
  57.  
  58. signed main()
  59. {
  60.     NeedForSpeed;
  61.     int t = 1;
  62.     // cin >> t;
  63.     while (t--)
  64.     {
  65.         solve();
  66.     }
  67.     return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment