fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<int> a;
  31.  
  32. int consistency1(int n){
  33.  
  34. int ans = 0;
  35. int maxDifSoFar = a[0] - a[1];
  36.  
  37. int maxi = a[0];
  38.  
  39. for(int k=2; k<n; k++){
  40.  
  41. ans = max(ans, maxDifSoFar * a[k]);
  42.  
  43. maxi = max(maxi, a[k-1]);
  44. maxDifSoFar = max(maxDifSoFar, maxi - a[k] );
  45.  
  46. }
  47.  
  48.  
  49. return ans;
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. //* Pivot j : prefix && sufix
  59.  
  60. int consistency2(int n){
  61.  
  62. int ans = 0;
  63. vector<int> sufixMaxi(n+1);
  64. for(int i=n-1; i>=0; i--){
  65. sufixMaxi[i] = max(sufixMaxi[i+1], a[i]);
  66. }
  67.  
  68. int maxi = a[0];
  69.  
  70. for(int j=1; j<=n-2; j++){
  71.  
  72. int rightMaxi = sufixMaxi[j+1];
  73.  
  74. int val = (maxi - a[j]) * rightMaxi;
  75.  
  76. maxi = max(maxi, a[j]);
  77.  
  78. ans = max(ans, val);
  79.  
  80. }
  81.  
  82.  
  83. return ans;
  84. }
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. int practice(int n){
  113.  
  114.  
  115. return 0;
  116. }
  117.  
  118.  
  119.  
  120.  
  121.  
  122. void solve() {
  123.  
  124. int n;
  125. cin>> n;
  126.  
  127. a.resize(n);
  128. for(int i=0; i<n; i++) cin >> a[i];
  129.  
  130. cout << consistency1(n) << " " << consistency2(n) << endl;
  131.  
  132.  
  133. }
  134.  
  135.  
  136.  
  137.  
  138.  
  139. int32_t main() {
  140. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  141.  
  142. int t = 1;
  143. // cin >> t;
  144. while (t--) {
  145. solve();
  146. }
  147.  
  148. return 0;
  149. }
Success #stdin #stdout 0s 5312KB
stdin
5
12 6 1 2 7 
stdout
77 77