Skip to content

Commit

Permalink
[D3] Title: 한빈이와 Spot Mart, Time: 1,699 ms, Memory: 25,108 KB -Baekjo…
Browse files Browse the repository at this point in the history
…onHub
  • Loading branch information
sey2 committed Nov 18, 2023
1 parent 93e3854 commit 870adb9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
15 changes: 15 additions & 0 deletions SWEA/D3/9229. 한빈이와 Spot Mart/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# [D3] 한빈이와 Spot Mart - 9229

[문제 링크](https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AW8Wj7cqbY0DFAXN)

### 성능 요약

메모리: 25,108 KB, 시간: 1,699 ms, 코드길이: 1,577 Bytes

### 제출 일자

2023-11-18 20:14



> 출처: SW Expert Academy, https://swexpertacademy.com/main/code/problem/problemList.do
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Solution {
static int max = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());

int cnt = 1;
while(T-->0) {
StringTokenizer st = new StringTokenizer(br.readLine());

int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());

int arr[] = new int[n];

st = new StringTokenizer(br.readLine());
for(int i=0; i<arr.length; i++)
arr[i] = Integer.parseInt(st.nextToken());

max = 0;

comb(arr, new boolean[n], 0, 2, m);

System.out.println("#" + cnt++ + " " + ((max == 0) ? -1 : max));
}
}

public static void comb(int arr[], boolean visited[], int start, int r, int m) {
if(r == 0) {
int sum = 0;

for(int i=0; i<arr.length; i++) {
if(visited[i]) {
sum += arr[i];
}
}

if(sum <= m) {
max = Math.max(sum, max);
}
return;
}

for(int i=start; i<arr.length; i++) {
if(!visited[i]) {
visited[i] = true;
comb(arr, visited, i+1, r-1, m);
visited[i] = false;
}
}
}

}

0 comments on commit 870adb9

Please sign in to comment.