-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path346-new-game.cpp
More file actions
65 lines (62 loc) · 1.62 KB
/
Copy path346-new-game.cpp
File metadata and controls
65 lines (62 loc) · 1.62 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
/*
* New Game [2025C]
* Problem: https://codeforces.com/problemset/problem/2025/C
* Verdict: ACCEPTED Solved: 2025-03-11
* Language: C++17 (GCC 7-32)
* Runtime: 312 ms Memory: 9100 KB
* Tags: binary search, brute force, greedy, implementation, sortings, two pointers
* Author: BidoTeima
* Source: https://codeforces.com/contest/2025/submission/309960264
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mxSubarraySum(vector<ll>&a, int k){
int n = (int)a.size();
deque<ll>dq;
ll ans = -1e18,cur=0,mn=1e18;
multiset<ll>ms;
ms.insert(0);
dq.push_back(0);
for(int i = 0; i < n; i++){
cur += a[i];
if(dq.size() == k){
ms.erase(ms.find(dq.front()));
dq.pop_front();
}
ans = max(ans, cur - *ms.begin());
dq.push_back(cur);
ms.insert(cur);
}
return ans;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
int n,k;
cin>>n>>k;
map<int,int>freq{};
vector<ll>v;
for(int i = 0; i < n; i++){
int x;
cin>>x;
freq[x]++;
}
ll ans = 0;
v.push_back(freq.begin()->second);
for(auto it = next(freq.begin()); it != freq.end(); ++it){
if(prev(it)->first + 1 != it->first){
ans=max(ans, mxSubarraySum(v, k+1));
v.clear();
}
v.push_back(it->second);
}
if(v.size())ans=max(ans,mxSubarraySum(v,k+1));
cout<<ans<<'\n';
}
return 0;
}