fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5.  
  6. int main() {
  7. ios::sync_with_stdio(false);
  8. cin.tie(nullptr);
  9.  
  10. freopen("TRIPLE.INP", "r", stdin);
  11. freopen("TRIPLE.OUT", "w", stdout);
  12.  
  13. int n;
  14. cin >> n;
  15. vector<ll> a(n);
  16. for (int i = 0; i < n; ++i) cin >> a[i];
  17.  
  18. vector<ll> max_left(n, -1), max_right(n, -1);
  19.  
  20. // Tính max_left
  21. ll current_max = a[0];
  22. for (int i = 1; i < n; ++i) {
  23. max_left[i] = current_max;
  24. current_max = max(current_max, a[i]);
  25. }
  26.  
  27. // Tính max_right
  28. current_max = a[n - 1];
  29. for (int i = n - 2; i >= 0; --i) {
  30. max_right[i] = current_max;
  31. current_max = max(current_max, a[i]);
  32. }
  33.  
  34. ll res = -1;
  35. for (int j = 1; j < n - 1; ++j) {
  36. if (max_left[j] >= a[j] && max_right[j] >= a[j]) {
  37. ll depth = max_left[j] + max_right[j] - 2 * a[j];
  38. res = max(res, depth);
  39. }
  40. }
  41.  
  42. cout << res << '\n';
  43. return 0;
  44. }
  45.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty