#include #define rep(i, a) for (int i = (int)0; i < (int)a; ++i) 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 = dp[0]; rep(j, a.size()){ t = min(t, dp[j]); dp[j] = t + abs(Y[i] - a[j]); } } printf("%d\n", *min_element(dp.begin(), dp.end())); return 0; }