-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[D3] Title: 한빈이와 Spot Mart, Time: 1,699 ms, Memory: 25,108 KB -Baekjo…
…onHub
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
||
} | ||
|