1060B - Maximum Sum of Digits - CodeForces Solution


greedy *1100

Please click on ads to support us..

Python Code:

n = int(input())

def max_input(n):
    if (n < 10):
        return n
    else:
        length = len(str(n)) - 1
        first_part = int("9" * length)
        second_part = (n - first_part)
        
                sum_first_part = 9 * length
        sum_second_part = 0

        for digit in str(second_part): 
            sum_second_part += int(digit)   

        result = sum_first_part + sum_second_part

        return result
        
        
print(max_input(n))

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#define fast_io ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define ll long long
#define ull unsigned ll
#define nrbits __builtin_popcount
#define nrbitsll __builtin_popcountll
#define int ll

const int dim = 2e5+1;
const int mod = 1e9+7;//998244353;
const double pi = 3.14159265359;
const int INF = INT_MAX;

// problema pe care o rezolv : https://www.pbinfo.ro/probleme/2485/nxy (cerinta 1)

int sum (int x)
{
    int ret = 0;
    while (x)
        ret += x % 10, x /= 10;
    return ret;
}

void solve()
{
    int n;
    cin >> n;

    int x = 0;

    while (x * 10 + 9 <= n)
        x = x * 10 + 9;

    cout << sum(x) + sum(n - x); // multumim enderdragon pentru aceasta pb mirobolanta!
}

signed main()
{
    fast_io;
    int t = 1;
    //cin >> t;

    for (int tc = 1; tc <= t; tc++)
        {
            //cout << "Case #" << tc << ": \n";
            solve(), cout << '\n';
        }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1091A - New Year and the Christmas Ornament
1352B - Same Parity Summands
1102A - Integer Sequence Dividing
630B - Moore's Law
1004A - Sonya and Hotels
1680B - Robots
1690A - Print a Pedestal (Codeforces logo)
1295A - Display The Number
1077A - Frog Jumping
1714G - Path Prefixes
1369C - RationalLee
289B - Polo the Penguin and Matrix
1716A - 2-3 Moves
1670B - Dorms War
1716B - Permutation Chain
987A - Infinity Gauntlet
1676G - White-Black Balanced Subtrees
1716D - Chip Move
1352F - Binary String Reconstruction
1487B - Cat Cycle
1679C - Rooks Defenders
56A - Bar
1694B - Paranoid String
35A - Shell Game
1684A - Digit Minimization
43B - Letter
1017A - The Rank
1698B - Rising Sand
235A - LCM Challenge
1075B - Taxi drivers and Lyft