349B - Color the Fence - CodeForces Solution


data structures dp greedy implementation *1700

Please click on ads to support us..

Python Code:

n = int(input())
a=list(map(int,input().split()))
m=a[0]
d=1
for i in range(9):
	if a[i]<=m:
		m=a[i]
		d=i+1
if m>n:
	print(-1)
	exit()
ans=list(str(d)*(n//m))
rem=n-m*(n//m)
for i in range(len(ans)):
	if rem<=0:
		break
	for j in range(8,d-1,-1):
		if a[j]-a[d-1]<=rem:
			ans[i]=str(j+1)
			rem-=a[j]-a[d-1]
			break
print(''.join(ans))

C++ Code:

#include <bits/stdc++.h>
using namespace std;
int v, a[10], min_digit, digits;
string ans;
int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> v;
  for(int i = 1; i <= 9; i++) {
    cin >> a[i];
    if(i == 1 || a[i] <= a[min_digit]) {
      min_digit = i;
    }
  }
  digits = v/a[min_digit];
  v -= (v/a[min_digit])*a[min_digit];
  //subtract the min digit from all a
  for(int i = 9; i >= min_digit; i--) {
    a[i] -= a[min_digit];
  }
  for(int i = 1; i <= digits; i++) {
    bool flag = false;
    for(int j = 9; j > min_digit; j--){
      if(v >= a[j]){
        v -= a[j];
        ans.push_back(j + '0');
        flag = true;
        break;
      }
    }
    if(!flag) {
      ans.push_back(min_digit + '0');
    }
  }
  if(digits == 0) {
    cout << -1;
  } else {
    cout << ans;
  }
}


Comments

Submit
0 Comments
More Questions

1547A - Shortest Path with Obstacle
624A - Save Luke
1238A - Prime Subtraction
1107C - Brutality
1391B - Fix You
988B - Substrings Sort
312A - Whose sentence is it
513A - Game
1711E - XOR Triangle
688A - Opponents
20C - Dijkstra
1627D - Not Adding
893B - Beautiful Divisors
864B - Polycarp and Letters
1088A - Ehab and another construction problem
1177B - Digits Sequence (Hard Edition)
1155B - Game with Telephone Numbers
1284A - New Year and Naming
863B - Kayaking
1395B - Boboniu Plays Chess
1475D - Cleaning the Phone
617B - Chocolate
1051B - Relatively Prime Pairs
95B - Lucky Numbers
1692D - The Clock
1553D - Backspace
1670D - Very Suspicious
1141B - Maximal Continuous Rest
1341A - Nastya and Rice
1133A - Middle of the Contest