#include #define rep(i, a) for (int i = (int)0; i < (int)a; ++i) template inline void chmin(T& a, T b) { if (a > b) { a = b; } } using namespace std; int main() { int N; scanf("%d", &N); vector Y(N), a(N); rep(i, N) { scanf("%d", &Y[i]); a[i] = Y[i]; } sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); vector dp(a.size(), 0); rep(i, N){ int t = 1 << 30; rep(j, a.size()){ chmin(t, dp[j] + abs(Y[i] - a[j])); dp[j] = t; } } printf("%d\n", *min_element(dp.begin(), dp.end())); return 0; }