#include #define M_PI 3.14159265358979323846 // pi using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector VI; typedef pair P; typedef tuple t3; typedef tuple t4; typedef tuple t5; #define rep(a,n) for(ll a = 0;a < n;a++) #define repi(a,b,n) for(ll a = b;a < n;a++) using namespace std; static const ll INF = 1e15; const ll mod = 1000000007; template void chmin(T & ref, const T value) { if (ref > value) ref = value; } int main() { int n; cin >> n; vector vs(n); rep(i, n) cin >> vs[i]; vector> dp(n + 1, vector(1e4 + 1, INF)); dp[0][0] = 0; rep(i, n) { const ll v = vs[i]; vector lows = dp[i]; rep(j, 1e4) { lows[j + 1] = min(lows[j + 1], lows[j]); } rep(j, 1e4 + 1) { if (j <= v) { chmin(dp[i + 1][j], lows[j] + v- j ); } else { chmin(dp[i + 1][j], dp[i][j] + j - v); } } } ll low = INF; rep(j, 1e4 + 1) { chmin(low, dp[n][j]); } cout << low << endl; return 0; }