Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions Seol-JY/202507/16 G4 다이어트.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
```java
import java.io.*;
import java.util.*;

public class Main {
static int N;
static int mp, mf, ms, mv;
static int[][] foods;
static int minCost = Integer.MAX_VALUE;
static List<Integer> result = new ArrayList<>();

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());

StringTokenizer st = new StringTokenizer(br.readLine());
mp = Integer.parseInt(st.nextToken());
mf = Integer.parseInt(st.nextToken());
ms = Integer.parseInt(st.nextToken());
mv = Integer.parseInt(st.nextToken());

foods = new int[N][5];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < 5; j++) {
foods[i][j] = Integer.parseInt(st.nextToken());
}
}

backtrack(0, 0, 0, 0, 0, 0, new ArrayList<>());

if (minCost == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(minCost);
for (int i = 0; i < result.size(); i++) {
if (i > 0) System.out.print(" ");
System.out.print(result.get(i));
}
System.out.println();
}
}

static void backtrack(int idx, int protein, int fat, int carbo, int vitamin,
int cost, List<Integer> selected) {
if (protein >= mp && fat >= mf && carbo >= ms && vitamin >= mv) {
if (cost < minCost) {
minCost = cost;
result = new ArrayList<>(selected);
}
return;
}

if (idx == N || cost >= minCost) return;

selected.add(idx + 1);
backtrack(idx + 1, protein + foods[idx][0], fat + foods[idx][1],
carbo + foods[idx][2], vitamin + foods[idx][3],
cost + foods[idx][4], selected);
selected.remove(selected.size() - 1);

backtrack(idx + 1, protein, fat, carbo, vitamin, cost, selected);
}
}
```