#include #include #include #include #include #include #include #include #include #include #include using namespace std; #define N (1000000000+7) #define M (998244353) #define INF 1e16 typedef long long ll; typedef pair P; template struct SegmentTree{ typedef function F; int n; F f; T unit; vector dat; SegmentTree(){}; SegmentTree(int newn,F f,T t):f(f),unit(t){ init(newn); } SegmentTree(const vector &v,F f,T t):f(f),unit(t){ int n_ = v.size(); init(n_); for(int i=0;i>=1) dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]); } T query(int a,int b){ T vl=unit,vr=unit; for(int l=a+n,r=b+n;l>=1,r>>=1){ if(l&1)vl=f(vl,dat[l++]); if(r&1)vr=f(dat[--r],vr); } return f(vl,vr); } }; SegmentTree dp[1010]; int main(void){ int n; cin>>n; vectora(n); for(int i=0;i>a[i]; auto f = [](ll a,ll b){ return min(a,b); }; for(int i=0;i<=n;i++){ if(i==0) { dp[i] = SegmentTree(10001,f,(ll)INF); dp[i].update(0,0); } else dp[i] = SegmentTree(10001,f,(ll)INF); } for(int i=0;iA+abs(a[i]-j)){ dp[i+1].update(j,A+abs(a[i]-j)); } } } ll res = (ll)INF; for(int i=0;i<10001;i++)res = min(dp[n].query(i,i+1),res); cout<