#include using namespace std; #ifdef LOCAL #include "debug.h" #else #define DEBUG(...) #endif template constexpr T inf = numeric_limits::max() / 2.1; auto chmin = [](auto&& a, auto b) { return b < a ? a = b, 1 : 0; }; constexpr int lim = 1e4; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector dp(lim + 1, 0); while (n--) { int a; cin >> a; vector ndp(lim + 1, inf); int mn = inf; for (int y = 0; y <= lim; ++y) { chmin(mn, dp[y]); ndp[y] = abs(y - a) + mn; } swap(dp, ndp); } cout << *min_element(begin(dp), end(dp)) << '\n'; }