Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Maximum Weight Difference codechef solution c++

#include<iostream>
#include<climits>
#include<algorithm>
using namespace std;

class solution
{
public:
	void solve()
	{
		int n, k;
		cin >> n >> k;
		int* arr = new int[n];
		for (int i = 0; i < n; i++)
		{
			cin >> arr[i];
		}
		sort(arr,arr + n);
		int sumAll = 0;
		for (int i = 0; i < n; i++)
		{
			sumAll += arr[i];
		}
		int kid = 0;
		for (int i = 0; i < k; i++)
		{
			kid += arr[i];
		}
		sort(arr, arr + n, greater<int>());
		int chef = 0;
		for (int i = 0; i < k; i++)
		{
			chef += arr[i];
		}
		cout << max(abs(kid - (sumAll - kid)), abs(chef - (sumAll - chef))) << "
";
		delete[] arr;
	}
};
int main()
{
	solution ss;

	int t;
	cin >> t;
	while (t--)
    {
		ss.solve();
	}

	return 0;
}
Source by www.codegrepper.com #
 
PREVIOUS NEXT
Tagged: #Maximum #Weight #Difference #codechef #solution
ADD COMMENT
Topic
Name
7+5 =