Problem Link
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define pb push_back
#define pi pair<int, int>
#define mp make_pair
bool sortByDescDiff(const pair<int, int> &p1, const pair<int, int> &p2)
{
return p1.second > p2.second;
}
int main()
{
int n, x, y;
int a[100001], b[100001];
long result = 0;
// stores absolute difference between tip gained by a and b
//first element in pair is order number and second is difference in tip
vector<pi> v;
cin >> n >> x >> y;
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
for(int i = 0; i < n; i++)
{
cin >> b[i];
}
for(int i = 0; i < n; i++)
{
int diff = abs(a[i] - b[i]);
v.pb(mp(i, diff));
}
sort(v.begin(), v.end(), sortByDescDiff);
int index = 0;
for(index = 0; index < n; index++)
{
int tip = v[index].first;
if(x > 0 && y > 0)
{
result += (a[tip] > b[tip]) ? a[tip] : b[tip];
(a[tip] > b[tip]) ? x-- : y--;
}
else
break;
}
if(x != 0)
{
while(index < n)
{
int tip = v[index].first;
result += a[tip];
index++;
}
}
if(y != 0)
{
while(index < n)
{
int tip = v[index].first;
result += b[tip];
index++;
}
}
cout << result << endl;
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define pb push_back
#define pi pair<int, int>
#define mp make_pair
bool sortByDescDiff(const pair<int, int> &p1, const pair<int, int> &p2)
{
return p1.second > p2.second;
}
int main()
{
int n, x, y;
int a[100001], b[100001];
long result = 0;
// stores absolute difference between tip gained by a and b
//first element in pair is order number and second is difference in tip
vector<pi> v;
cin >> n >> x >> y;
for(int i = 0; i < n; i++)
{
cin >> a[i];
}
for(int i = 0; i < n; i++)
{
cin >> b[i];
}
for(int i = 0; i < n; i++)
{
int diff = abs(a[i] - b[i]);
v.pb(mp(i, diff));
}
sort(v.begin(), v.end(), sortByDescDiff);
int index = 0;
for(index = 0; index < n; index++)
{
int tip = v[index].first;
if(x > 0 && y > 0)
{
result += (a[tip] > b[tip]) ? a[tip] : b[tip];
(a[tip] > b[tip]) ? x-- : y--;
}
else
break;
}
if(x != 0)
{
while(index < n)
{
int tip = v[index].first;
result += a[tip];
index++;
}
}
if(y != 0)
{
while(index < n)
{
int tip = v[index].first;
result += b[tip];
index++;
}
}
cout << result << endl;
return 0;
}
Comments
Post a Comment