Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
1.2 kB
2
Indexable
Never
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 100005;

int main()
{
    string s;
    cin >> s;
    int sum = 0;
    for(int i = 0; i < s.length(); i++) {
        sum += (s[i] - '0');
    }
    string najgolem = "";
    for(int i = 0; i < s.length(); i++) {
        int digit = s[i] - '0';
        for(int j = 0; j <= 9; j++) {
            if(i == 0 and j != 0 and j != (s[i] - '0')) {
                sum -= (s[i] - '0');
                sum += j;
                if(sum % 3 == 0) {
                    s[i] = (j + '0');
                    najgolem = max(najgolem, s);
                    s[i] = (digit + '0');
                }
                sum -= j;
                sum += digit;
            }
            else if(i > 0 and digit != j) {
                sum -= (s[i] - '0');
                sum += j;
                if(sum % 3 == 0) {
                    s[i] = (j + '0');
                    najgolem = max(najgolem, s);
                    s[i] = (digit + '0');
                }
                sum -= j;
                sum += digit;
            }
        }
    }
    cout << najgolem << endl;
    return 0;
}

/*
 6 50
 12 |13 14 12| 11 11
 
 */