-
Notifications
You must be signed in to change notification settings - Fork 0
/
UsbAndPS.java
91 lines (83 loc) · 2.25 KB
/
UsbAndPS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* https://codeforces.com/problemset/problem/762/B
#greedy #two-pointer #sorting
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class UsbAndPS {
public static boolean helper(int idx1, int idx2, int[] inputArr) {
if (inputArr[idx1] > 0) {
inputArr[idx1] -= 1;
return true;
} else if (inputArr[idx2] > 0) {
inputArr[idx2] -= 1;
return true;
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] inputArr = new int[3];
for (int i = 0; i < inputArr.length; i++) {
int tmp = sc.nextInt();
inputArr[i] = tmp;
}
int m = sc.nextInt();
ArrayList<Long> usbMouseArr = new ArrayList<>();
ArrayList<Long> psMouseArr = new ArrayList<>();
int[] countArr = new int[2];
for (int i = 0; i < m; i++) {
long cost = sc.nextLong();
String name = sc.next();
if (name.equals("USB")) {
usbMouseArr.add(cost);
} else {
psMouseArr.add(cost);
}
}
Collections.sort(usbMouseArr);
Collections.sort(psMouseArr);
long[] sumArr = new long[2]; // 0 - sum of mouse, 1 - sum of cost.
int idxUsb = 0;
int idxPS = 0;
while (idxUsb < usbMouseArr.size() && idxPS < psMouseArr.size()) {
if (inputArr[0] == 0 && inputArr[1] == 0 && inputArr[2] == 0) break;
if (psMouseArr.get(idxPS) < usbMouseArr.get(idxUsb)) {
if (helper(1, 2, inputArr)) {
sumArr[0] += 1;
sumArr[1] += psMouseArr.get(idxPS);
idxPS++;
} else {
break;
}
} else {
if (helper(0, 2, inputArr)) {
sumArr[0] += 1;
sumArr[1] += usbMouseArr.get(idxUsb);
idxUsb++;
} else {
break;
}
}
}
while (idxUsb < usbMouseArr.size()) {
if (helper(0, 2, inputArr)) {
sumArr[0] += 1;
sumArr[1] += usbMouseArr.get(idxUsb);
idxUsb++;
} else {
break;
}
}
while (idxPS < psMouseArr.size()) {
if (helper(1, 2, inputArr)) {
sumArr[0] += 1;
sumArr[1] += psMouseArr.get(idxPS);
idxPS++;
} else {
break;
}
}
System.out.println(sumArr[0] + " " + sumArr[1]);
}
}