ENG  RUS Timus Online Judge
Online Judge
Problems
Authors
Online contests
Site news
Webboard
Problem set
Submit solution
Judge status
Guide
Register
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

## Discussion of Problem 1005. Stone Pile

WA#8. Give me some tests
Posted by Ilya 12 Feb 2020 00:21
#include<bits/stdc++.h>
#include<string>

using namespace std;

const double PI = 3.14159265358979323846;

int mass[20];

int main()
{
int n, sum1, sum2, ans = 1e9, left, right;
cin >> n;

for(int i = 0; i < n; i++)
{
cin >> mass[i];
}

for(int i = 0; i < n; i++)
{
sort(mass, mass + n);

sum1 = 0, sum2 = 0, left = 0, right = i;

while(left < i)
{
//cout << mass[left] << " ";
sum1 += mass[left];
left++;
}

while(right < n)
{
//cout << mass[right] << " ";
sum2 += mass[right];
right++;
}

//cout << endl;
ans = min(ans, abs(sum1 - sum2));

for(int j = 0; j < i; j++)
{

for(int k = i; k < n; k++)
{
sum1 = 0, sum2 = 0, left = 0, right = i;

swap(mass[j], mass[k]);

while(left < i)
{
//cout << mass[left] << " ";
sum1 += mass[left];
left++;
}

while(right < n)
{
//cout << mass[right] << " ";
sum2 += mass[right];
right++;
}
//cout << endl;
ans = min(ans, abs(sum1 - sum2));

swap(mass[j], mass[k]);
}
}
}

cout << ans;

return 0;
}