-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhayfeast.cpp
58 lines (54 loc) · 1.4 KB
/
hayfeast.cpp
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef map<ll, ll> mll;
void mllAdd(mll &spicy, int add){
if(spicy.find(add) == spicy.end()){
spicy.insert(pll(add, 1));
}else{
spicy[add]++;
}
}
void mllRemove(mll &spicy, int rm){
spicy[rm]--;
if(spicy[rm] == 0){
spicy.erase(rm);
}
}
int main(){
freopen("hayfeast.in", "r", stdin);
freopen("hayfeast.out", "w", stdout);
ll N, M; cin >> N >> M;
pll food[100000];
for(int i = 0; i < N; i++){
cin >> food[i].first >> food[i].second;
}
mll spicy;
ll taste = 0, start, beginning = 0;
for(int i = 0; i < N && taste < M; i++){
taste += food[i].first;
mllAdd(spicy, food[i].second);
start = i;
}
mll::iterator last = spicy.end();
ll minspicy = (--last) -> first;
taste -= food[beginning].first;
mllRemove(spicy, food[beginning].second);
beginning++;
for(int i = start + 1; i < N; i++){
taste += food[i].first;
mllAdd(spicy, food[i].second);
if(taste >= M){
last = spicy.end();
minspicy = min(minspicy, (--last) -> first);
}
while(taste >= M){
taste -= food[beginning].first;
mllRemove(spicy, food[beginning].second);
beginning++;
}
}
cout << minspicy << endl;
return 0;
}