Source Code
#include <bits/stdc++.h>
using namespace std;
    
int main (){
    
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n, l, r, k;
  cin >> n >> l >> r >> k, --l, --r;
  int a[n];
  for (int i = 0; i < n; ++i) {
  	cin >> a[i];
  }
  a[l] += k;
  double mx = -1e9;
  int ans = 2e9, c[n];
  c[r] = 0;
  for (int i = r + 1; i < n; ++i) {
  	c[i] = c[i - 1] + max(0, a[i] - a[i - 1]);
  }
  for (int i = r - 1; ~i; --i) {
  	c[i] = c[i + 1] + max(0, a[i] - a[i + 1]);
  }
  for (int i = l + 1; i < n; ++i) {
  	double cur = (a[i] - a[l]) * 1. / (i - l);
  	if (cur >= mx) {
  		ans = min(ans, c[i]);
  	}
  	mx = max(mx, cur);
  }
  mx = -1e9;
  for (int i = l - 1; ~i; --i) {
  	double cur = (a[i] - a[l]) * 1. / (l - i);
  	if (cur >= mx) {
  		ans = min(ans, c[i]);
  	}
  	mx = max(mx, cur);
  }
  cout << ans;

  return 0;
}
Copy
Frisbee Towers SuhaibSawalha1
GNU G++17
15 ms
1.8 MB
Accepted