/* -*- coding: utf-8 -*- * * 3157.cc: No.3157 Nabeatsu - yukicoder */ #include #include #include using namespace std; /* constant */ const int MAX_L = 1000000; /* typedef */ /* global variables */ char s[MAX_L + 4]; /* subroutines */ void dec(int l, char s[]) { int co = 1; for (int i = l - 1; co && i >= 0; i--) { if (s[i] == '0') s[i] = '9'; else s[i]--, co = 0; } } int dig3(int l, char s[]) { for (int i = 0; i < l; i++) if (s[i] == '3') return i; return -1; } bool multiple3(int l, char s[]) { int sum = 0; for (int i = 0; i < l; i++) sum += (s[i] - '0'); return (sum % 3 == 0); } /* main */ int main() { scanf("%s", s); int l = strlen(s); for (;;) { //puts(s); int d3 = dig3(l, s); //printf(" d3=%d\n", d3); if (d3 >= 0) { s[d3] = '2'; for (int i = d3 + 1; i < l; i++) s[i] = '9'; } if (multiple3(l, s)) dec(l, s); else break; } puts(s); return 0; }