Skip to content

Commit

Permalink
B.SummationGame.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
akshat-khosya committed Jan 22, 2024
1 parent c96b278 commit 607e3ad
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"Local: B","url":"/home/akshat/Documents/C_plus_plus/codeforces/1000-1200/B.SummationGame.cpp","tests":[{"id":1705860310903,"input":"8\n1 1 1\n1\n4 1 1\n3 1 2 4\n6 6 3\n1 4 3 2 5 6\n6 6 1\n3 7 3 3 32 15\n8 5 3\n5 5 3 3 3 2 9 9\n10 6 4\n1 8 2 9 3 3 4 5 3 200\n2 2 1\n4 3\n2 1 2\n1 3\n","output":"0\n2\n0\n3\n-5\n-9\n0\n-1\n"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"/home/akshat/Documents/C_plus_plus/codeforces/1000-1200/B.SummationGame.cpp","group":"local","local":true}
47 changes: 47 additions & 0 deletions codeforces/1000-1200/B.SummationGame.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Author: Akshat Khosya
#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0)
#define fs cin.tie(0)
#define ll long long int
#define pb push_back
#define fr(i, n) for (int i = 0; i < n; i++)
#define fm(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
void akshat()
{
int n, k, x;
cin >> n >> k >> x;
vector<int> v(n);
fr(i, n) cin >> v[i];
sort(v.begin(), v.end(), greater<int>());
vector<int> prefixSum(n + 1);
prefixSum[0] = 0;
for (int i = 1; i <= n; i++)
{
prefixSum[i] = prefixSum[i - 1] + v[i - 1];
}
int sum = INT_MIN;
for (int i = 0; i <= k; i++)
{
sum = max(sum, prefixSum[n] - 2 * (prefixSum[min(i + x, n)]) + prefixSum[i]);
}
cout << sum << endl;
}
int main()
{
fast;
fs;
int t;
cin >> t;
while (t--)
{
akshat();
}
return 0;
}
/*
1. Bob Want to maximize the sum and alice want to minimize the sum.
2. Alice will make all element negative form highest order.
3. Now sort the array and calculate prefix sum.
4. for every k check for max sum and return it.
*/

0 comments on commit 607e3ad

Please sign in to comment.