#include #define rep(i, n) for (int i = 0; i < (n); i++) #define all(x) (x).begin(), (x).end() using ll = long long; using namespace std; template using vec = std::vector; int main() { int N; cin >> N; vec Y(N); rep(i, N) cin >> Y[i]; int ans = 0; rep(i, N) { int b = Y[i] - Y[max(i - 1, 0)]; int n = Y[i] - Y[min(N - 1, i + 1)]; if (b < 0) { ans += abs(b); Y[i] = Y[max(i - 1, 0)]; } else if (b >= 0 && n >= 0 && b <= n) { ans += abs(b); Y[i] = Y[max(i - 1, 0)]; } else if (b >= 0 && n >= 0 && b >= n) { ans += abs(n); Y[i] = Y[min(N - 1, i + 1)]; } } cout << ans << endl; }