Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- typedef long long ll;
- typedef unsigned long long ull;
- #define pb push_back
- #define vll vector<ll>
- #define vi vector<int>
- #define tests int t; cin >> t; while(t--)
- using namespace std;
- vll a(18);
- ll dfs(ll n, ll k) {
- ll m = n/a[k];
- n = n%a[k];
- if(n == 0) return m*k;
- return m*k+min(k+dfs(a[k]-n, k-1), dfs(n, k-1));
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL); cout.tie(NULL);
- ll n; cin >> n;
- a[0] = 0;
- for(int i = 1; i < 18; i++) {
- a[i] = a[i-1]*10LL+1;
- }
- cout << dfs(n, 17);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement