Official

B - Buy One Carton of Milk Editorial by en_translator


Since you need at most \(100\) eggs, at most \(100\) packs of each kind is needed . Thus, the problem can be solved by enumerating the number of each pack from \(0\) to \(100\).

Sample code (C++)

#include<bits/stdc++.h>
using namespace std;

int main(){
  int n, s, m, l;
  cin >> n >> s >> m >> l;

  int ans = 100000000;
  for(int a=0; a<=100; a++){
    for(int b=0; b<=100; b++){
      for(int c=0; c<=100; c++){
        if(a*6 + b*8 + c*12 >= n){
          ans=min(ans, a*s + b*m + c*l);
        }
      }
    }
  }
  cout << ans << endl;
}

Sample code (Python)

N, S, M, L = map(int,input().split())

ans = 10**8
for a in range(101):
  for b in range(101):
    for c in range(101):
      if a*6 + b*8 + c*12 >= N:
        ans = min(ans, a*S + b*M + c*L)

print(ans)

posted:
last update: