题目描述
In the Kingdom of AtCoder, only banknotes are used as currency. There are 10100+1 kinds of banknotes, with the values of 1,10,102,103,…,10(1010). You have come shopping at a mall and are now buying a takoyaki machine with a value of N. (Takoyaki is the name of a Japanese snack.)
To make the payment, you will choose some amount of money which is at least N and give it to the clerk. Then, the clerk gives you back the change, which is the amount of money you give minus N.
What will be the minimum possible number of total banknotes used by you and the clerk, when both choose the combination of banknotes to minimize this count?
Assume that you have sufficient numbers of banknotes, and so does the clerk.
Constraints
·N is an integer between 1 and 101,000,000 (inclusive).
输入
Input is given from Standard Input in the following format:
N
输出
Print the minimum possible number of total banknotes used by you and the clerk.
样例输入
【样例1】
36
【样例2】
91
【样例3】
314159265358979323846264338327950288419716939937551058209749445923078164062862089986280348253421170
样例输出
【样例1】
8
【样例2】
3
【样例3】
243
提示
样例1解释
If you give four banknotes of value 10 each, and the clerk gives you back four banknotes of value 1 each, a total of eight banknotes are used.
The payment cannot be made with less than eight banknotes in total, so the answer is 8.
样例2解释
If you give two banknotes of value 100,1, and the clerk gives you back one banknote of value 10, a total of three banknotes are used.
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize (2)
#pragma G++ optimize (2)
#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
//#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
const ll inf = 1e15;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
int num[maxn];
int main()
{
/**int n=read;
int flag=1;
for(int i=1;i<=n;i++)
{
num[i]=read;
if(num[i]%2==0){
if(num[i]%3!=0&&num[i]%5!=0)
flag=0;
}
}
if(flag==0) printf("DENIED\n");
if(flag) printf("APPROVED\n");**/
/**
int n=read;
string ss;
map<string,int>mp;
for(int i=0;i<n;i++)
{
///scanf("%s",ss);
cin>>ss;
mp[ss]++;
}
int cnt=0;
for(auto& v:mp) cnt=max(cnt,v.second);
for(auto& v:mp){
if(v.second==cnt)
///cout<<v.first<<endl;
printf("%s\n",v.first);
}**/
string ss;
cin >> ss;
vector<int > a;
int len = ss.length();
for(int i=0;i<len;i++)
a.push_back(ss[len-i-1]-'0');
a.push_back(0);
ll ans = 0;
for(int i=0;i<len;i++)
{
if(a[i] >= 10)
a[i+1]++;
else if(a[i] < 10-a[i])
ans += a[i];
else if(a[i] > 10-a[i]){
ans += 10-a[i];
a[i+1]++;
}else{
if(a[i+1] > 4){
ans += 10-a[i];
a[i+1]++;
}
else ans += a[i];
}
}
ans += a[len];
cout << ans << endl;
return 0;
}
/**ll kruskal(){
sort(num,num+m,cmp);
for(ll i=1;i<=n;i++) num2[i]=i;
ll res=0,cnt=0;
for(ll i=0;i<m;i++){
ll aa=num[i].a,b=num[i].b,w=num[i].w;
aa=searchnum(aa),b=searchnum(b);
if(aa!=b){
num2[aa]=b;
res+=w;
cnt++;
}
}
if(cnt<n-1) return inf;
else return res;
}**/
/**************************************************************
Language: C++
Result: 正确
Time:59 ms
Memory:11976 kb
****************************************************************/