Official

B - Buy One Carton of Milk Editorial by kyopro_friends


必要な卵の個数が高々 \(100\) 個であることから、どのパックも高々 \(100\) 個買えば十分です。よって、各パックを買う個数を \(0\) から \(100\) まで全探索することでこの問題を解くことができます。

実装例(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;
}

実装例(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: