Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 57364b5

Browse files
committed
배수빈: [BOJ] 2258 정육점_250103
1 parent 1b62c1d commit 57364b5

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

‎BOJ/1000-5000번/SB_2258.java‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.Arrays;
5+
import java.util.StringTokenizer;
6+
7+
public class SB_2258 {
8+
static int N, M;
9+
static int[][] dp;
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
StringTokenizer st = new StringTokenizer(br.readLine());
13+
14+
N = Integer.parseInt(st.nextToken());
15+
M = Integer.parseInt(st.nextToken());
16+
17+
dp = new int[N][2];
18+
for (int i = 0; i < N; i++) {
19+
st = new StringTokenizer(br.readLine());
20+
dp[i][0] = Integer.parseInt(st.nextToken()); // 무게
21+
dp[i][1] = Integer.parseInt(st.nextToken()); // 가격
22+
}
23+
24+
// 가격 오름차순, 무게 내림차순
25+
Arrays.sort(dp, (o1, o2) -> {
26+
if (o1[1] != o2[1]) return o1[1] - o2[1];
27+
return o2[0] - o1[0]; // 가격이 같을땐 무게 기준 내림차순
28+
});
29+
30+
31+
int total = 0;
32+
int price = 0;
33+
int mn = Integer.MAX_VALUE;
34+
boolean flag = false;
35+
36+
for (int i = 0; i < N; i++) {
37+
total += dp[i][0]; // 무게 계속 누적 됨
38+
39+
if (i > 0 && dp[i-1][1] == dp[i][1]) // 같은 가격의 고기는 돈 지불
40+
price += dp[i][1];
41+
else price = dp[i][1]; // 다른 가격이면, 이 가격으로 지금까지 고기 다 살 수 있음(가격 오름차순 정렬)
42+
43+
if (total >= M){
44+
flag = true;
45+
mn = Math.min(mn, price); // 동일한 가격이 누적됐을때, 새로운 값이 더 쌀 수 있음
46+
}
47+
}
48+
49+
System.out.println(flag ? mn : -1);
50+
51+
}
52+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /