-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhaybale.java
47 lines (47 loc) · 1.54 KB
/
haybale.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
import java.io.*;
import java.util.*;
public class haybale {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
long need = Long.parseLong(st.nextToken());
long[] f = new long[n];
long[] s = new long[n];
for(int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
f[i] = Long.parseLong(st.nextToken());
s[i] = Long.parseLong(st.nextToken());
}
int left = 0;
long ret = Long.MAX_VALUE;
TreeMap<Long, Integer> seen = new TreeMap<Long, Integer>();
long flavor = 0;
for(int i = 0; i < n; i++) {
flavor += f[i];
update(seen, s[i], 1);
while(flavor - f[left] >= need) {
update(seen, s[left], -1);
flavor -= f[left++];
}
if(flavor >= need) {
ret = Math.min(ret, seen.lastKey());
}
}
pw.println(ret);
pw.close();
}
private static void update(Map<Long, Integer> m, long k, int v) {
if(!m.containsKey(k)) {
m.put(k, 0);
}
int nv = m.get(k) + v;
if(nv == 0) {
m.remove(k);
}
else {
m.put(k, nv);
}
}
}