1467C - Three Bags - CodeForces Solution


constructive algorithms greedy *1900

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(), x.end()
#define nl '\n'
#define int long long
#define ll long long
#define pii pair<ll, ll>
#define mod 998244353  // 998244353  1000000007
using namespace std;
void solve()
{
	int n1, n2, n3;
	cin >> n1 >> n2 >> n3;
	int a[n1], b[n2], c[n3];
	int sum = 0, sum1 = 0, sum2 = 0, sum3 = 0;
	for(int i = 0; i < n1; i++)
	{
		cin >> a[i];
		sum += a[i];
		sum1 += a[i];
	}
	for(int i = 0; i < n2; i++)
	{
		cin >> b[i];
		sum += b[i];
		sum2 += b[i];
	}
	for(int i = 0; i < n3; i++)
	{
		cin >> c[i];
		sum += c[i];
		sum3 += c[i];
	}
	sort(a, a + n1);
	sort(b, b + n2);
	sort(c, c + n3);
	int ans = 0;
	if(n1 == 1)
	{
		ans = max(ans, sum - 2LL*a[0]);
	}
	if(n2 == 1)
	{
		ans = max(ans, sum - 2LL*b[0]);
	}
	if(n3 == 1)
	{
		ans = max(ans, sum - 2LL*c[0]);
	}
	ans = max(ans, sum - 2LL*(a[0] + b[0]));
	ans = max(ans, sum - 2LL*(a[0] + c[0]));
	ans = max(ans, sum - 2LL*(c[0] + b[0]));
	if(n1 > 1 and n1 <= (n2 + n3 - 1))
	{
		ans = max(ans, sum - 2LL*(sum1));
	}
	if(n2 > 1 and n2 <= (n1 + n3 - 1))
	{
		ans = max(ans, sum - 2LL*(sum2));
	}
	if(n3 > 1 and n3 <= (n1 + n2 - 1))
	{
		ans = max(ans, sum - 2LL*(sum3));
	}
	ans = max(ans, sum1 - (sum2 + sum3));
	ans = max(ans, sum2 - (sum1 + sum3));
	ans = max(ans, sum3 - (sum2 + sum1));
	ans = max(ans, sum2 + sum3 - sum1);
	ans = max(ans, sum1 + sum2 - sum3);
	ans = max(ans, sum3 + sum1 - sum2);
	cout << ans << nl;
}
signed main()
{
//	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	int t1 = 1;
//	cin >> t1;
	while(t1--)
	{
		solve();
	}
}


Comments

Submit
0 Comments
More Questions

1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game