#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class WhiteSpaceEditing { public:
int getMinimum(vector <int> lines)
{
const int N = lines.size();
const int V = *max_element(lines.begin(), lines.end());
vector<int> dp(V+1), dp_neo(V+1);
for(int v=0; v<=V; ++v)
dp[v] = abs(v - lines[N-1]);
for(int i=N-2; i>=0; --i)
{
int best = INT_MAX;
for(int v=lines[i]; v>=0; --v) {
best = min(best, dp[v]);
dp_neo[v] = abs(v - lines[i]) + best;
}
best = INT_MAX;
for(int v=lines[i]; v<=V; ++v) {
best = min(best, dp[v]);
dp_neo[v] = abs(v - lines[i]) + best;
}
dp.swap(dp_neo);
}
return dp[0] + N-1;
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, WhiteSpaceEditing().getMinimum(lines));}
int main(){
CASE(0)
int lines_[] = { 3, 2, 3 };
vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_));
int _ = 6;
END
CASE(1)
int lines_[] = { 0 };
vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_));
int _ = 0;
END
CASE(2)
int lines_[] = { 1, 2, 4 }
;
vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_));
int _ = 6;
END
CASE(3)
int lines_[] = { 250, 105, 155, 205, 350 }
;
vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_));
int _ = 499;
END
CASE(4)
int lines_[] = {0};
vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_));
int _ = 0;
END
CASE(5)
int lines_[] = {1};
vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_));
int _ = 1;
END
CASE(5)
int lines_[] = {10};
vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_));
int _ = 10;
END
CASE(5)
int lines_[] = {10,10};
vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_));
int _ = 11;
END
CASE(5)
int lines_[] = {1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000};
vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_));
int _ = 1000049;
END
}
// END CUT HERE