File tree Expand file tree Collapse file tree 1 file changed +60
-0
lines changed
Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static int n;
7+ static int [] str, weight;
8+ static int answer = 0 ;
9+
10+ public static void main (String [] args ) throws IOException {
11+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
12+ n = Integer . parseInt(br. readLine());
13+ str = new int [n];
14+ weight = new int [n];
15+
16+ for (int i = 0 ; i < n; i++ ) {
17+ StringTokenizer st = new StringTokenizer (br. readLine());
18+ str[i] = Integer . parseInt(st. nextToken());
19+ weight[i] = Integer . parseInt(st. nextToken());
20+ }
21+
22+ dfs(0 );
23+ System . out. println(answer);
24+ }
25+
26+ static void dfs (int idx ) {
27+ if (idx == n) {
28+ int broken = 0 ;
29+ for (int i = 0 ; i < n; i++ ) {
30+ if (str[i] <= 0 ) broken++ ;
31+ }
32+ answer = Math . max(answer, broken);
33+ return ;
34+ }
35+
36+ if (str[idx] <= 0 ) {
37+ dfs(idx + 1 );
38+ return ;
39+ }
40+
41+ boolean hit = false ;
42+
43+ for (int i = 0 ; i < n; i++ ) {
44+ if (i == idx || str[i] <= 0 ) continue ;
45+
46+ hit = true ;
47+
48+ str[idx] -= weight[i];
49+ str[i] -= weight[idx];
50+
51+ dfs(idx + 1 );
52+
53+ str[idx] += weight[i];
54+ str[i] += weight[idx];
55+ }
56+
57+ if (! hit) dfs(idx + 1 );
58+ }
59+ }
60+ ```
You can’t perform that action at this time.
0 commit comments