Spaces:
Running
Running
[["s = input()\nn = len(s)\nind = -1\nf = False\nfor i in range(n):\n if s[i] == '[':\n f = True\n elif s[i] == ':':\n if f:\n ind = i\n break\nbind = -1\nf = False\nfor i in range(n-1,-1,-1):\n if s[i] == ']':\n f = True\n elif s[i] == ':':\n if f:\n bind = i\n break\n# print(ind,bind)\nif ind == -1 or bind == -1:\n print(-1)\nelif ind >= bind:\n print(-1)\nelse:\n ans = 4\n for i in range(ind+1,bind):\n if s[i] == '|':\n ans += 1\n print(ans)\n", "def main():\n s = input()\n \n if s.count('[') == 0 or s.count(']') == 0:\n print(-1)\n return\n \n t = s[s.find('['):s.rfind(']')+1]\n \n if t.count(':') < 2:\n print(-1)\n return\n \n t = t[t.find(':'):t.rfind(':')+1]\n print(4 + t.count('|'))\n\nmain()", "s = input()\nif '[' in s:\n s = s[s.find('[') + 1:]\n if ']' in s:\n s = s[:s.rfind(']')]\n if s.count(':') >= 2:\n s = s[s.find(':') + 1 : s.rfind(':')]\n print(s.count('|') + 4)\n\n else:\n print(-1)\n else:\n print(-1)\nelse:\n print(-1)", "import sys\ns = input()\nst = s.find('[')\nif st==-1: print((-1)); return\ns = s[st+1:]\n#print(s)\nst = s.find(':')\nif st==-1: print((-1)); return\ns = s[st+1:]\n#print(s)\ns = s[::-1]\nst = s.find(']')\nif st==-1: print((-1)); return\ns = s[st+1:]\n#print(s)\nst = s.find(':')\nif st==-1: print((-1)); return\ns = s[st+1:]\n#print(s)\nx = s.count('|')\nprint(x+4 if x>=0 else -1)\n", "s = input()\n\nsb,eb,sc,ec = -1, -1, -1, -1\n\nfor i in range(len(s)):\n\tif s[i] == '[' and sb == -1:\n\t\tsb = i\n\telif s[i] == ']':\n\t\teb = i\n\telif s[i] == ':' and sc == -1 and sb!=-1:\n\t\tsc = i\n\nif eb <= sb or sc>eb:\n\tprint(-1)\nelif sb ==-1 or eb==-1 or sc==-1:\n\tprint(-1)\nelse:\n\tfor i in range(sc+1, eb):\n\t\tif s[i] == ':':\n\t\t\tec = i\n\tif ec == -1:\n\t\tprint(-1)\n\telse:\n\t\tcnt = 0\n\t\tfor i in range(sc,ec):\n\t\t\tif (s[i] == '|'):\n\t\t\t\tcnt += 1\n\t\tprint(cnt+4)", "s = input()\nt_d = 0\ntry:\n left = -1\n was_b = False\n for i in range(len(s)):\n if s[i] == '[' and not was_b:\n was_b = True\n continue\n if s[i] == ':' and was_b:\n left = i\n break\n t_d += 1\n if left == -1:\n raise ArithmeticError()\n right = -1\n was_b = False\n for i in range(len(s) - 1, -1, -1):\n if s[i] == ']' and not was_b:\n was_b = True\n continue\n if s[i] == ':' and was_b:\n right = i\n break\n t_d += 1\n if right == -1 or right <= left:\n raise ArithmeticError()\n for i in range(left + 1, right):\n if s[i] != '|':\n t_d += 1\n print(len(s) - t_d)\nexcept:\n print(-1)\n \n", "s = input()\n\nmode = 0\nl = len(s)\nr = -1\nfor i in range(len(s)):\n if mode == 0:\n if s[i] == \"[\":\n mode = 1\n if mode == 1:\n if s[i] == \":\":\n l = i\n break\n\nmode = 0\nfor i in range(len(s)-1, -1, -1):\n if mode == 0:\n if s[i] == \"]\":\n mode = 1\n if mode == 1:\n if s[i] == \":\":\n r = i\n break\n \nif l >= r:\n print(-1)\nelse:\n c = 0\n for i in range(l+1, r):\n if s[i] == \"|\":\n c += 1\n print(c+4)\n", "s = input()\n\nf1 = False\nf2 = False\nl1 = -1\nfor l in range(len(s)):\n if f1 == False and s[l] == '[':\n f1 = True\n elif f1 == True and s[l] == ':':\n f2 = True\n l1 = l\n break\ng1 = False\ng2 = False\nr1 = -1\nfor r in range(len(s) - 1, -1, -1):\n if g1 == False and s[r] == ']':\n g1 = True\n elif g1 == True and s[r] == ':':\n g2 = True\n r1 = r\n break\nif (l1 == -1 or r1 == -1) or (r1 <= l1):\n print(-1)\n \nelse:\n ans = 4\n for i in range(l1 + 1, r1):\n if s[i] == '|': ans += 1\n print(ans)", "s=input()\npos1=-1\npos2=-1\npos3=-1\npos4=-1\nfor i in range(0,len(s)):\n if(s[i]=='['):\n pos1=i\n break\nfor i in range(len(s)-1,pos1,-1):\n if(s[i]==']'):\n pos2=i\n break\nfor i in range(pos1,pos2+1):\n if(s[i]==':'):\n pos3=i\n break\nfor i in range(pos2,pos3,-1):\n if(s[i]==':'):\n pos4=i\n break\n \nif(pos1==-1 or pos2==-1 or pos3==-1 or pos4==-1 or len(s)<4):\n print('-1')\nelse:\n c=0\n for j in range(pos3,pos4):\n if(s[j]=='|'):\n c=c+1\n print(c+4)\n", "def ii():\n return int(input())\ndef mi():\n return list(map(int, input().split()))\ndef li():\n return list(mi())\n\ns = input().strip()\nn = len(s)\nans = -1\nfb = s.find('[')\nif fb >= 0:\n fc = s.find(':', fb)\n if fc >= 0:\n lb = s.rfind(']')\n if lb > fc:\n lc = s.rfind(':', 0, lb)\n if lc > fc:\n ans = 4 + s[fc:lc].count('|')\nprint(ans)\n", "s = input()\n\ndef sovle(s):\n\n i1 = s.find('[')\n if i1 == -1:\n return -1\n s = s[i1+1:]\n i2 = s.find(':')\n if i2 == -1:\n return -1\n\n s = s[i2+1 :]\n i1 = s.rfind(']')\n if i1 == -1:\n return -1\n s = s[:i1]\n i2 = s.rfind(':')\n if i2 == -1:\n return -1\n s = s[:i2]\n x = s.count('|')\n return x+4\n\nprint(sovle(s))", "def solve(s):\n if s.find('[') == -1:\n return -1\n s = s[s.find('['):]\n #print(s)\n if s.find(':') == -1:\n return -1\n s = s[s.find(':') + 1:]\n #print(s)\n if s.find(']') == -1:\n return -1\n s = s[:s.rfind(']')]\n #print(s)\n if s.find(':') == -1:\n return -1\n s = s[:s.rfind(':')]\n #print(s)\n return s.count('|') + 4\n\ns = input()\nprint(solve(s))", "s=input()\ni=s.find('[')\nif i==-1:\n print(-1)\n return\ns=s[i:]\ni=s.rfind(']')\n\nif i==-1:\n print(-1)\n return\ns=s[:i+1]\nl,h=0,0\nfor i,d in enumerate(s):\n if d==':':\n l=i\n break\nfor i,d in enumerate(s):\n if d==':':\n h=i\nif l==h:\n print(-1)\n return\nc=0\nfor i in range(l+1,h):\n if s[i]=='|':\n c+=1\nprint(c+4)\n", "from sys import stdin\ns=stdin.readline().strip()\nx=-1\nfor i in range(len(s)):\n if s[i]==\"[\":\n x=i\n break\ny=-1\nfor i in range(len(s)-1,-1,-1):\n if s[i]==\"]\":\n y=i\n break\nif x==-1 or y==-1 or y<x:\n print(-1)\n return\nx1=-1\nfor i in range(x,y):\n if s[i]==\":\":\n x1=i\n break\ny1=-1\nfor i in range(y-1,x,-1):\n if s[i]==\":\":\n y1=i\n break\nif x1==-1 or y1==-1 or y1<=x1:\n print(-1)\n return\nans=4\nfor i in range(x1,y1):\n if s[i]==\"|\":\n ans+=1\nprint(ans)\n", "s = str(input().strip())\ni = 0\nn = len(s)\nwhile i < n and s[i] != '[':\n i+=1\nif(i == n):\n print(-1)\n return\nj = n-1\nwhile j > i and s[j] != ']':\n j-=1\nif(j <= i):\n print(-1)\n return\nwhile i < j and s[i] != ':':\n i+=1\nif(i == j):\n print(-1)\n return\nwhile j > i and s[j] != ':':\n j-=1\nif(j == i):\n print(-1)\n return\nk = i+1\nc = 0\nwhile k < j:\n if(s[k] == '|'):\n c+=1\n k+=1\nprint(c+4)\n", "import sys\ns = input()\nl = len(s)\ns_list = [x for x in s]\n\ncounter = 0\ntry:\n\ta = s_list.index('[')\n\tcounter += a\n\ts_list = s_list[a + 1:]\nexcept:\n\tprint(-1)\n\treturn\n\ntry:\n\ta = s_list.index(':')\n\tcounter += a\n\ts_list = s_list[a + 1:]\nexcept:\n\tprint(-1)\n\treturn\n\ns_list_rev = s_list.copy()\ns_list_rev.reverse()\n\ntry:\n\tb = s_list_rev.index(']')\n\tcounter += b\n\ts_list_rev = s_list_rev[b+1:]\nexcept:\n\tprint(-1)\n\treturn\n\ntry:\n\tb = s_list_rev.index(':')\n\tcounter += b\n\ts_list_rev = s_list_rev[b+1:]\nexcept:\n\tprint(-1)\n\treturn\ns_list_rev = [x for x in s_list_rev if x != '|']\ncounter += len(s_list_rev)\nprint(l - counter)", "MOD = 10**9 + 7\nI = lambda:list(map(int,input().split()))\n\ns = input()\nres = 0\nn = len(s)\nst = -1\ne = -1\nfor i in range(n):\n if s[i] == '[':\n st = i\n break\nfor i in range(n-1, -1, -1):\n if s[i] == ']':\n e = i\n break\n# print(st , e)\nif st > e or st == -1 or e == -1:\n print(-1)\n return\na = -1\nb = -1\nfor i in range(st, e):\n if s[i] == ':':\n a = i\n break\nfor i in range(e, st, -1):\n if s[i] == ':':\n b = i\n break\nif a == b or a == -1 or b == -1:\n print(-1)\n return\ncount = 0\nfor i in range(a, b):\n if s[i] == '|':\n count += 1\nprint(4 + count)", "s=input()\nst=\"\"\nidx=-1\nfor i in range(len(s)):\n if s[i]=='[':\n idx=i\n break\nif idx==-1:\n print(-1)\n return\nidxl=-1\nfor i in range(len(s)-1,-1,-1):\n if s[i]==']' and i>idx:\n idxl=i\n break\nif idxl==-1:\n print(-1)\n return\ncol=col2=-1\nfor i in range(len(s)):\n if s[i]==':' and i>idx and i<idxl:\n col=i\n break\nif col==-1:\n print(-1)\n return\nfor i in range(len(s)-1,-1,-1):\n if s[i]==':' and i>col and i<idxl:\n col2=i\n break\nif col2==-1:\n print(-1)\n return\nans=0\nfor i in range(col+1,col2):\n if s[i]=='|':\n ans+=1\nprint(4+ans)\n \n\n\n", "s = input()\nrev = s[::-1]\n\nleft = s.find(\"[\")\nif left != -1:\n left = s.find(\":\", left)\n\nright = rev.find(\"]\")\nif right != -1:\n right = rev.find(\":\", right)\n\nif left == -1 or right == -1:\n print(-1)\n return\nright = len(s)-right-1\nif left >= right:\n print(-1)\n return\n\nprint(4 + s[left:right].count(\"|\"))\n", "def ba(s):\n c1 = s.find('[')\n c2 = s.find(':', c1+1)\n c3 = s.rfind(']', c2+1)\n c4 = s.rfind(':', c2+1, c3)\n if -1 in [c1, c2, c3, c4]:\n return -1\n return s.count('|', c2, c4)+4\n\n\nprint(ba(input()))\n\n", "s = input()\nif '[' in s and ']' in s:\n a = s.index('[') + 1\n b = len(s)-s[::-1].index(']') - 1\nelse:\n print(-1)\n return\ns = s[a:b]\nif s.count(':') >= 2:\n a = s.index(':')+1\n b = len(s)-s[::-1].index(':')-1\nelse:\n print(-1)\n return\nc = 0\nfor el in s[a:b]:\n if el =='|':\n c += 1\nprint(4 + c)", "s = input()\n\nb = [0]*len(s)\n\nob = 0\ncc = 0\np = -1\nq = -1\n\ncount = 0\n\nfor ind,c in enumerate(s):\n if c == '[':\n ob = 1\n elif c == ':' and p >= 0:\n q = ind\n elif c == ':' and ob == 1 and p < 0:\n p = ind\n elif c == ']' and q >= 0:\n cc = q\n elif c == '|':\n count += 1\n b[ind] = count\n\nif cc > 0:\n print( 4 + b[cc]-b[p])\nelse:\n print(-1)\n", "s = input()\nif '[' in s and ']' in s and ':' in s:\n e = s.count(':')\n if e<2:\n print(-1)\n else:\n a = s.index('[')\n b = len(s)-1-s[::-1].index(']')\n if b<a:\n print(-1)\n else:\n if s[a+1:b].count(':')<2:\n print(-1)\n else:\n st1 = True\n count = 0\n for i in range(a+1, b):\n if st1 and s[i]==':':\n pos1 = i\n st1 = False\n if s[i]==':':\n pos2 = i\n \n for i in range(pos1+1, pos2):\n if s[i]=='|':\n count+=1\n \n print(count+4)\nelse:\n print(-1) ", "s=input()\ni1=-1\ni2=-1\nk1=-1\nk2=-1\nc=0\nfor i in range(len(s)):\n if(s[i]=='['):\n i1=i\n break\nfor i in range(len(s)-1,-1,-1):\n if(s[i]==']'):\n i2=i\n break\nfor i in range(i1,i2+1):\n if(s[i]==':'):\n k1=i\n break\nfor i in range(i2,i1-1,-1):\n if(s[i]==':'):\n k2=i\n break\nfor i in range(k1,k2+1):\n if(s[i]=='|'):\n c+=1\n\nif(i1==-1 or i2==-1 or i1>=i2 or k1==-1 or k2==-1 or k1==k2):\n print(-1)\nelse:\n print(4+c)", "s = input()\nl = 0\nend = 0\ni = 1\n\nwhile i <= len(s):\n if l == 0 and s[-i] == ']':\n l += 1\n elif l == 1 and s[-i] == ':':\n l += 1\n end = len(s) - i\n break\n i += 1\n\nif l < 2:\n print(-1)\n return\n\nfor i in range(0, end):\n if l >= 4 and s[i] == '|':\n l += 1\n elif l == 2 and s[i] == '[':\n l += 1\n elif l == 3 and s[i] == ':':\n l += 1\n\nif l >= 4:\n print(l)\nelse:\n print(-1)"], ["num = list(map(int, input()))\nbest = num[:]\nfor i in range(-1, -len(num) - 1, -1):\n if num[i] == 0:\n continue\n num[i] -= 1\n for j in range(i + 1, 0):\n num[j] = 9\n if sum(num) > sum(best):\n best = num[:]\ns = ''.join(map(str, best)).lstrip('0')\nprint(s)\n", "s_num = input()\nnum = int(s_num)\ndigs = [int(s_num[i]) for i in range(len(s_num))]\n\nmax_sum = sum(digs)\nres = num\nfor i in range(len(s_num)):\n if (digs[i] != 0):\n digs[i] -= 1\n n_sum = sum(digs[:i + 1]) + 9 * (len(s_num) - i - 1)\n if n_sum >= max_sum:\n n_res = int(''.join([str(digs[i]) for i in range(i + 1)]) + '9' * (len(s_num) - i - 1))\n if (n_sum == max_sum):\n res = max(n_res, res)\n else:\n res = n_res\n max_sum = n_sum\n\n digs[i] += 1\nprint(res)\n", "a=int(input())\nif(a//10==0):\n print(a)\n return\nk=9\nwhile(k<a):\n k=k*10+9\nif(k==a):\n print(k)\nelse:\n k//=10\n k=int(str(a)[0]+str(k))\n i=len(str(k))-1\n z=k\n while(z>a):\n z=int(str(k)[0:i]+str(int(str(k)[i])-1)+str(k)[i+1:len(str(k))])\n i-=1\n print(z) ", "x = int(input())\nif x < 10:\n print(x)\nelif x == int(str(x)[0] + '9'*(len(str(x))-1)):\n print(x)\nelse:\n a = str(x)[0] + '9' * (len(str(x)) - 1)\n a = list(a)\n for i in range(len(a) - 1, -1, -1):\n k = a[i]\n a[i] = str(int(a[i]) - 1)\n if x >= int(''.join(a)):\n print(int(''.join(a)))\n break\n a[i] = k\n", "def sum_str(y):\n return sum(map(int, str(y)))\n\n\nx = input()\nlength = len(x)\nbad_answer = str(int(x[0]) - 1) + '9' * (length - 1) \ntotal = sum_str(bad_answer)\n\n\nif length == 1 or sum_str(x) >= total:\n print(x)\nelse:\n for i in range(length - 1, 0, -1):\n new_total = 9 * (length - i)\n new_answer = str(int(x[:i]) - 1)\n new_total += sum_str(new_answer)\n\n if new_total >= total:\n new_answer = new_answer if new_answer != '0' else ''\n print(new_answer + '9' * (length - i))\n break\n else:\n print(bad_answer)\n", "import sys\n\ndef calc(s):\n res =0\n for c in s:\n res+= int(c)\n return res\n\n\ns = list(sys.stdin.readline().rstrip())\nbest = \"\".join(s) \ncount = calc(s)\n\ni = len(s)-1\nwhile i!=0:\n i-=1\n if s[i+1]!= '9':\n s[i+1] = '9'\n while s[i]=='0':\n s[i]='9'\n i-=1\n s[i] = chr(ord(s[i])-1)\n c = calc(s)\n if count < c:\n count = c\n best = \"\".join(s)\n\nif best[0] == '0':\n best = best[1:]\n\nprint(best)", "x = input()\nn = len(x)\nif n == 1:\n print(x)\n return\nans = \"\"\ns = 0\nps = 0\npn = \"\"\nfor i in range(n):\n ts = ps + int(x[i]) - 1 + 9 * (n - i - 1)\n if ts >= s:\n ans = pn + str(int(x[i]) - 1) + \"9\" * (n - i - 1)\n s = ts\n ps += int(x[i])\n pn += x[i]\nif ps >= s:\n ans = pn\nprint(int(ans))", "n = int(input())\n\ndef f(numb):\n lst = [numb]\n cap = 10\n\n while numb // cap > 0:\n lst.append((numb // cap - 1) * cap + cap - 1)\n cap *= 10\n\n return lst\n\ndef g(numb):\n lst = []\n while numb != 0:\n lst.append(numb % 10)\n numb //= 10\n\n return lst\n\n\nmaximum = max([sum(g(i)) for i in f(n)])\n\nmaximum = [i for i in f(n) if maximum == sum(g(i))]\n\nprint(max(maximum))", "\"\"\" Created by Shahen Kosyan on 3/11/17 \"\"\"\n\ndef __starting_point():\n x = input()\n\n if int(x) < 10:\n print(x)\n return\n\n arr = [int(a) for a in list(x)]\n x_sum = sum(arr)\n\n i = len(arr) - 1\n answer = ''\n while i > 0:\n if arr[i] != 9 and arr[i] != 8:\n arr[i - 1] -= 1\n answer = '9' + answer\n else:\n change = False\n for j in range(i - 1, 0, -1):\n if arr[j] < 9:\n change = True\n break\n\n if arr[i] == 8 and change:\n answer = '9' + answer\n arr[i - 1] -= 1\n else:\n if not change:\n answer = str(arr[i]) + answer\n else:\n answer = '9' + answer\n\n if i == 1 and arr[0] != 0:\n answer = str(arr[0]) + answer\n i -= 1\n\n answer = [int(a) for a in list(answer)]\n if x_sum == sum(answer):\n print(x)\n else:\n answer = [str(a) for a in answer]\n print(''.join(answer))\n\n__starting_point()", "x=input()\nl=len(x)\nx=int(x)\ns='9'*l\nsx=str(x)\nm=int(s)\nc=0\nwhile c!=1:\n if m>x:\n m=m-10**(l-1)\n else:\n c=1\nsm=str(m)\nmm=[] \nfor i in range(len(sm)):\n mm.append(int(sm[i]))\nxx=[] \nfor i in range(l):\n xx.append(int(sx[i]))\nif m==x:\n print(m)\nelif sum(xx)==sum(mm):\n print(x)\nelse:\n k=len(xx)-1\n while k>=0:\n if sum(xx)<sum(mm):\n if xx[k]==9:\n k-=1\n else:\n xx[k]=9\n xx[k-1]-=1\n k-=1\n else:\n if xx[0]==0:\n xx.remove(0)\n for b in range(len(xx)):\n xx[b]=str(xx[b])\n ww=''.join(xx)\n print(ww)\n break", "x = input()\nvariants = [x] + [str(int(x[:i]) - 1) +\n '9' * (len(x) - i) for i in range(1, len(x))]\nprint(int(max(variants, key=lambda x: (sum(map(int, x)), int(x)))))\n", "def sum_div(n):\n summa = 0\n while n > 0:\n summa = summa + n % 10\n n = n // 10\n return summa\n\n\ndef run(n):\n l_n = len(n)\n left = ''\n if l_n > 2 and '9' * l_n != n and n[1] == '9' and '9' * (l_n - 1) != n[1:]:\n left = n[0]\n n = n[1:]\n while l_n > 1 and n[1] == '9':\n left += n[1]\n n = n[1:]\n l_n = len(n)\n l_n = len(n)\n if len(n) == 1:\n return n\n elif '9' * (l_n - 1) == n[1:]:\n return left + n\n elif n[0] != '1':\n min_number = int(str(int(n[0]) - 1) + '9' * (l_n - 1))\n if sum_div(min_number) > sum_div(int(n)):\n return left + str(min_number)\n else:\n return left + n\n else:\n min_number = int('9' * (l_n - 1)) if l_n > 1 else 0\n if sum_div(min_number) > sum_div(int(n)):\n return left + str(min_number)\n else:\n return left + n\n\n\nn = input()\nprint(run(n))\n", "#This code is dedicated to Olya S.\n\ndef e(x):\n s=0\n while x>0:\n s+=x%10\n x//=10\n return s\n\ndef down(x):\n l=len(x)-1\n return str(int(x[0])-1)+'9'*l\n\nn=input()\nif len(n)>1 and n[1]=='9':\n print(n[0],end='')\n n=n[1:]\n while len(n)>1 and n[0]=='9' and n[1]=='9':\n print('9',end='')\n n=n[1:]\n\nif e(int(n))>=e(int(down(n))):\n print(n)\nelse:\n print(int(down(n)))\n\n \n \n\n\n\n \n\n", "def sum_n(n):\n l = len(n)\n\n summ = 0\n for i in range(l):\n summ += int(n[i])\n\n return summ\n\ndef transfer(x, i):\n x = list(x)\n \n x[i+1] = '9'\n if x[i] != '0':\n x[i] = str(int(x[i])-1)\n else:\n j = i\n while (j > 0) and (int(x[j]) == 0):\n x[j] = '9'\n j -= 1\n x[j] = str(int(x[j])-1)\n if (x[0] == '0'):\n del x[0]\n\n return x\n\nx = list(input())\nmax_cifr = sum_n(x)\nmaxnum = x\nres = ''\n\nfor i in range(len(x)-2, -1, -1):\n x = transfer(x, i)\n if(max_cifr < sum_n(x)):\n max_cifr = sum_n(x)\n maxnum = x\n\nfor i in range(len(maxnum)):\n res = res+maxnum[i]\n \nprint(res)\n", "x = input()\nsum = 0\nfor i in x:\n temp = int(i)\n sum += temp\n\nxlen = len(x)\none = int(x[0])\ntry:\n two = int(x[1])\nexcept:\n two = 0\n\nif (two == 9):\n count = 1\n for i in range(1, xlen):\n z = int(x[i])\n if (z == 9):\n count = i\n else:\n break\n answ = x[0:count] + \"8\" + (\"9\" * (xlen - count - 1))\nelif (one == 1):\n answ = '9' * (xlen - 1)\nelse:\n answ = str((one - 1)) + (\"9\" * (xlen-1))\n\nansw = str(answ)\nsumansw = 0\nfor i in answ:\n temp = int(i)\n sumansw += temp\n\nif (sum >= sumansw):\n print(x)\nelse:\n print(answ)", "def sum1(x): # \u043f\u043e\u0434\u0441\u0447\u0451\u0442 \u0441\u0443\u043c\u043c\u044b \u0446\u0438\u0444\u0440 \u0447\u0438\u0441\u043b\u0430 x\n summa = 0\n for i in x:\n summa += int(i)\n return summa\n\n\nx = input()\nc = sum1(x)\nresult = int(x)\nn = len(x) - 1\nj = n\nfor i in range(0, n):\n if x[i] != '0':\n ni = int(x[i]) - 1 # \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u044e i-\u044b\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u043d\u0430 1\n xi = x[0:i] + str(ni) + '9' * j # \u0441\u0442\u0440\u043e\u044e \u043d\u043e\u0432\u043e\u0435 \u0447\u0438\u0441\u043b\u043e\n j -= 1\n ci = sum1(xi)\n if c < ci:\n c = ci\n result = int(xi)\n elif c == ci and result < int(xi):\n result = int(xi)\n else:\n j -= 1\n continue\nprint(result)\n", "def f(n, k):\n n = str(n)\n if n[k] == \"0\":\n return f(n, k - 1)\n a = []\n for i in n:\n a.append(int(i))\n n = a\n n[k] = int(n[k]) - 1\n n[k + 1::] = [9] * (len(n) - k - 1)\n return n\na = input()\nn = len(a)\nans = [int(x) for x in a]\nms = sum(ans)\nfor i in range(0, n):\n ca = f(a, i)\n cs = sum(ca)\n if cs> ms:\n ans = ca\n ms = cs\n elif cs == ms:\n if int(''.join([str(_) for _ in ca])) > int(''.join([str(_) for _ in ans])):\n ans = ca\nprint(int(''.join([str(_) for _ in ans])))", "n = int(input().strip())\n\ns = []\nwhile n > 0:\n s.append(n % 10)\n n //= 10\ns = s[::-1]\n\nn = len(s)\nans = 0\nbest = -1\nfor i in range(n):\n res = sum(s[:i + 1]) - 1 + 9 * (n - i - 1)\n if res >= ans:\n ans = res\n best = i\n\ndef get(s, pos):\n ans = 0\n for i in range(len(s)):\n if i > pos:\n ans = ans * 10 + 9\n else:\n ans = ans * 10 + s[i]\n if i == pos:\n ans -= 1\n return ans\n\nif sum(s) >= ans:\n print(get(s, n))\nelse:\n print(get(s, best))\n\n", "def main():\n\n\tdef sum(x):\n\t\tres = 0\n\n\t\twhile x > 0:\n\t\t\tres += x % 10\n\t\t\tx //= 10\n\n\t\treturn res\n\n\tn = input()\n\tfirst = n[0]\n\tp = [1]\n\n\tfor i in range(1, 20):\n\t\tp.append(p[-1] * 10)\n\n\tdata = []\t\n\tfor i in range(len(n)):\n\t\tif i > 0 and n[i] == '0':\n\t\t\tcontinue\n\t\ttemp = n[:i] + str(max(0, int(n[i]) - 1)) + \"9\"* (len(n) - i - 1)\n\t\tdata.append((sum(int(temp)), int(temp)))\n\n\tdata.append((sum(int(n)), int(n)))\n\t\n\tdata.sort(reverse=True)\n\n\tprint(data[0][1])\n\n\treturn\n\ndef __starting_point():\n\tmain()\n__starting_point()", "def cnt_sum(str_num):\n\tsum = 0\n\tfor a in str_num:\n\t\tsum += ord(a) - ord('0')\n\treturn sum\n\nstr_a = input().strip()\nmax_sum = cnt_sum(str_a)\nans = str_a\ncnt_digit = len(str_a)\n\nfor i in range(cnt_digit - 1, -1, -1):\n\tif str_a[i] != '0':\n\t\tnew_str = str_a[:i] + chr(ord(str_a[i]) - 1) + '9'*(cnt_digit - i - 1)\n\t\tcur_sum = cnt_sum(new_str)\n\t\tif cur_sum > max_sum:\n\t\t\tmax_sum = cur_sum\n\t\t\tans = new_str\n\nprint(int(ans))\n", "def summaX(x):\n k=0\n for el in x:\n k+=int(el)\n return k\nn=input();N=[];Z=[]\nfor el in n:\n N.append(el)\nz=summaX(N)\nZ=N.copy()\nfor i in range(1,len(N)):\n if int(N[i])!=9:\n N[i-1]=int(N[i-1])-1\n for j in range(i,len(n)):\n N[j]=9\nif z>=summaX(N):\n for el in Z:\n print(el,end='')\nelse:\n if N[0]==0:\n N.pop(0)\n for el in N:\n print(el,end='')\n", "n = int(input())\n\ndef sumd(n):\n\tj = n\n\tsumn = 0\n\twhile j:\n\t\tsumn += j % 10\n\t\tj //= 10\n\treturn sumn\n\nj = n\nstrn = str(n)\nl = len(strn)\nsumn = sumd(n)\n\nstra = [i for i in str(n)]\ni = 1\nwhile i < l and stra[i] == '9':\n\ti += 1\nif (i != l):\n\tstra[i - 1] = str(int(stra[i - 1]) - 1)\n\twhile i < l:\n\t\tstra[i] = '9'\n\t\ti += 1\n\nss = ''\nfor i in range(l):\n\tss += stra[i]\nif ss[0] == '0':\n\tss = ss[1:]\nsn = int(ss)\n\nif sn < n and sumd(sn) <= sumn:\n\tss = strn\n\tsn = n\n\nprint(ss)\n", "from random import randint\n\ndef f(s):\n a = 0\n for i in s:\n a += int(i)\n return a\n\ndef solve(n):\n n1 = list(str(n))\n ans = 0\n maxx = 0\n for i in range(len(n1)):\n n2 = n1[:i] + [str(int(n1[i]) - 1)] + ['9' for j in range(len(n1) - i - 1)]\n if f(n2) >= maxx:\n maxx = f(n2)\n ans = n2\n if f(n1) >= maxx:\n maxx = f(n1)\n ans = n1\n return [int(''.join(ans)), maxx]\n\ndef tl(n):\n ans = 0\n maxx = 0\n for i in range(1, n + 1):\n if f(list(str(i))) >= maxx:\n maxx = f(list(str(i)))\n ans = i\n return [ans, maxx]\n\n'''for kkk in range(100):\n n = randint(1, 10 ** 5)\n c1 = solve(n)\n c2 = tl(n)\n if c1 != c2:\n print(n)\n print(c1)\n print(c2)\nprint('ok')'''\nn = int(input())\nprint(solve(n)[0])\n", "a = [1, 2, 3, 4, 5, 6, 7, 8, 9]\nfor length in range(2, 30):\n for first in range(1, 10):\n for pos in range(1, length):\n a.append(int(str(first) + '9' * (pos - 1) + '8' + '9' * (length - pos - 1)))\n a.append(int(str(first) + '9' * (length - 1)))\n \nn = int(input())\nl = 0\nr = len(a)\nwhile l < r - 1:\n middle = (l + r) // 2\n if (a[middle] <= n):\n l = middle\n else:\n r = middle\n \nprint(a[l])", "def get(s):\n ans = 0\n for i in s:\n ans += (ord(i) - ord('0'))\n return ans\n\n\ndef solve1():\n x = input()\n n = len(x)\n best_ans = x\n best_val = get(x)\n ans = str('' if int(x[0]) - 1 == 0 else int(x[0]) - 1) + '9' * (n - 1)\n if get(ans) > best_val or (get(ans) >= best_val and int(ans) > int(best_ans)):\n best_ans = ans\n best_val = get(ans)\n for i in range(1, n):\n #print(ans)\n ans = x[:i] + str(int(x[i]) - 1) + '9' * (n - i - 1)\n if get(ans) > best_val or (get(ans) >= best_val and int(ans) > int(best_ans)):\n best_ans = ans\n best_val = get(ans)\n return best_ans\n \nbest = [0] * 10000\ndef solve2():\n nonlocal best\n was = 0\n for i in range(1, 10000):\n if get(str(i)) >= was:\n best[i] = i\n was = get(str(i))\n else:\n best[i] = best[i - 1]\n \ndef stress():\n solve2()\n for i in range(1, 10000):\n if int(solve1(str(i))) != best[i]:\n print(i, best[i], solve1(str(i)))\n\n#stress()\nprint(solve1())"]] |