#include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; #define int long long typedef vector VI; typedef pair pii; typedef priority_queue PQ; templatebool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } #define fore(i,a) for(auto &i:a) #define REP(i,n) for(int i=0;i > dp; //vector > > vvvi; //dp=vector >(N, vector(M,0)); //vector > v; //v.push_back(make_pair(x,y)); //priority_queue, greater > q2; signed main() { cin.tie(0); ios::sync_with_stdio(false); int dp[1000010][2]; string S; cin >> S; reverse(S.begin(), S.end()); S += '0'; int N = S.size(); REP(i, N + 1)REP(j, 2)dp[i][j] = INF; dp[0][0] = 0; REP(i, N) { REP(j, 2) { int x = S[i] - '0'; x += j; REP(a, 10) { int ni = i + 1, nj = 0; int b = a - x; if (b < 0) { nj = 1; b += 10; } chmin(dp[ni][nj], dp[i][j] + a + b); } } } cout << dp[N][0] << endl; return 0; }