Search
 
SCRIPT & CODE EXAMPLE
 

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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: css window id 
Cpp :: short int range in c++ 
Cpp :: c++ profiling tools 
Cpp :: unambiguous 
Cpp :: CPP print executable name 
Cpp :: Vaccine Dates codechef solution in c++ 
Cpp :: C++ Initializing a thread with a class/object 
Cpp :: time_t c++ stack overflow 
Cpp :: Minimizing the dot product codechef in c++ 
Cpp :: c++ camera capture 
Cpp :: qtextedit no line break 
Cpp :: kruskal algorithm 
Cpp :: increment integer 
Cpp :: How To Calculate 1+1 in c++ 
Cpp :: c++ program for inflation rate of two numbers 
Cpp :: pimpl c++ 
Cpp :: cpp get keystroke in console only 
Cpp :: the question for me 
Cpp :: Qt asynchronous HTTP request 
Cpp :: delete[] cpp 
Cpp :: file is good in c++ 
Cpp :: command loop ctrl D c++ 
Cpp :: flowchart to display factors of a number 
Cpp :: delete an dynamic array 
Cpp :: access the element of vector point2f c++ 
Cpp :: char to binary 
Cpp :: find largest number in each row in array c++ using function 
Cpp :: glm multiply vector by scalar 
Cpp :: c++ program to use nmap 
Cpp :: c++ hsl to rgb integer 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =