Maximize array sum by X increments when each element is divided by 10 | C ++ program for the above problem ; initialize variables ; add the current contribution of the element to the answer ; if the value is already maximum then we can 't change it ; moves required to move to the next multiple of 10 ; no of times we can add 10 to this value so that its value does not exceed 1000. ; sort the array ; adding the values to increase the numbers to the next multiple of 10 ; if the total moves are less than X then increase the answer ; if the moves exceed X then we cannot increase numbers ; if there still remain some moves ; remaining moves ; add minimim of increments and remaining / 10 to the answer ; output the final answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximizeval10 ( int a [ ] , int n , int k ) { int increments = 0 ; int ans = 0 ; vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) { ans += ( a [ i ] / 10 ) ; if ( a [ i ] == 1000 ) continue ; else { v . push_back ( 10 - a [ i ] % 10 ) ; increments += ( 100 - ( ( a [ i ] ) / 10 ) - 1 ) ; } } sort ( v . begin ( ) , v . end ( ) ) ; int sum = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { sum += v [ i ] ; if ( sum <= k ) { ans ++ ; } else break ; } if ( sum < k ) { int remaining = k - sum ; ans += min ( increments , remaining / 10 ) ; } cout << ans ; } int main ( ) { int N = 4 ; int X = 6 ; int A [ N ] = { 4 , 8 , 8 , 8 } ; maximizeval10 ( A , N , X ) ; return 0 ; }