-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewYrGift.java
More file actions
81 lines (81 loc) · 2.76 KB
/
NewYrGift.java
File metadata and controls
81 lines (81 loc) · 2.76 KB
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
import java.io.*;
import java.util.*;
public class NewYrGift {
static class Friend implements Comparable<Friend> {
int id;
int x, y, z;
long cost;
public Friend(int id, int x, int y, int z) {
this.id = id;
this.x = x;
this.y = y;
this.z = z;
this.cost = (long)z - y;
}
public int compareTo(Friend other) {
return Integer.compare(other.x, this.x);
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer st;
String line = br.readLine();
if (line == null) return;
int t = Integer.parseInt(line);
while (t-- > 0) {
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
long k = Long.parseLong(st.nextToken());
int[] a = new int[m];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < m; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(a);
Friend[] friends = new Friend[n];
long sumY = 0;
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int z = Integer.parseInt(st.nextToken());
friends[i] = new Friend(i, x, y, z);
sumY += y;
}
long currK = k - sumY;
if (currK < 0) {
currK = 0;
}
Arrays.sort(friends);
PriorityQueue<Long> AssCosts = new PriorityQueue<>();
ArrayList<Long> PCosts = new ArrayList<>();
int avlBox = 0;
int boxPtr = m - 1;
for (Friend f : friends) {
while (boxPtr >= 0 && a[boxPtr] >= f.x) {
avlBox++;
boxPtr--;
}
AssCosts.add(f.cost);
if (AssCosts.size() > avlBox) {
long evCost = AssCosts.poll();
PCosts.add(evCost);
}
}
int happyCnt = AssCosts.size();
Collections.sort(PCosts);
for (long cost : PCosts) {
if (currK >= cost) {
currK -= cost;
happyCnt++;
} else {
break;
}
}
out.println(happyCnt);
}
out.flush();
}
}