#include #define rep(i,n) for (int i = 0; i < n; ++i) #define ALL(c) (c).begin(), (c).end() #define SUM(x) std::accumulate(ALL(x), 0LL) #define MIN(v) *std::min_element(v.begin(), v.end()) #define MAX(v) *std::max_element(v.begin(), v.end()) #define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end()) using namespace std; using ll = long long; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } const int INF = 1e9; const long long INFLL = 1LL<<60; ll calc(vector& a) { ll ans = 0; int n = a.size(); for (int i = 0; i < a.size()-2; i++) { if (a[i] < a[i+1] && a[i+1] > a[i+2]) { // a < b > c -> b を max(a,c) にする ans += a[i+1] - max(a[i], a[i+2]); a[i+1] = max(a[i], a[i+2]); } else if (a[i] > a[i+1]) { ans += a[i] - a[i+1]; a[i+1] = a[i]; } } if (a[n-1] < a[n-2]) { ans += a[n-2] - a[n-1]; a[n-1] = a[n-2]; } return ans; } int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector a(n); rep(i, n) cin >> a[i]; ll ans = INFLL; for (int i = 0; i < 1000; i++) { auto b = a; ll tmp = i; b[0] -= i; tmp += calc(b); chmin(ans, tmp); } cout << ans << endl; return 0; }