-
Notifications
You must be signed in to change notification settings - Fork 0
/
hackercup_20_running_on_fumes-1.cpp
95 lines (82 loc) · 1.77 KB
/
hackercup_20_running_on_fumes-1.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
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
92
93
94
95
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
void solv()
{
ll n, m;
cin >> n >> m;
vector<ll> arr(n);
for (int i = 0; i < n; ++i)
{
cin >> arr[i];
}
ll ans = 0;
priority_queue<pair<ll, ll>> q;
/*
M = 3
-1 9 16 17 -9 19 -19 17
0 1 4 7 0 5 9 8 0 3 0 6
0 0 0 0 1 4 7 9 9 16 19 0
heap now = 7 5 9
*/
// q.push({-11111111, 0});
// for(int i = 0 ;)
if(n-1 <= m){
cout << 0 << endl;
return;
}
for(int i = 0 ; i <= m ; ++i){
if(arr[i] == 0 ){
q.push({INT64_MIN , i});
}
else{
q.push({-arr[i] , i});
}
}
if(q.top().first == INT64_MIN){
cout << -1 << endl;
return;
}
for (int i = m+1; i < n; ++i)
{
while (q.top().second < i - m)
{
q.pop();
}
ans = q.top().first;
// else
// {
if (q.top().first == INT64_MIN)
{
q.push({INT64_MIN, i});
// continue;
}
else
{
if(arr[i] == 0){
q.push({INT64_MIN , i});
}
else{
auto x = q.top().first;
q.push({x - arr[i], i});
}
}
// }
// cout << i << " " << ans << endl;
// dp[i] = min(dp[i] , )
}
// return ans;
cout << (ans == INT64_MIN ? -1 : -ans) << endl;
}
int main()
{
freopen("running_on_fumes_chapter_1_input.txt", "r", stdin);
freopen("output.txt" , "w" , stdout);
int tt = 1;
cin >> tt;
for (int i = 1; i <= tt; ++i)
{
cout << "Case #"<<i <<": ";
solv();
}
}