I am trying to solve this problem but I can’t understand why my solution is failing. What Am I missing?
My Code:
#include
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
ll n, k;
vector shelf1, shelf2, temp;
cin >> n >> k;
ll n1, n2;
n1 = n;
n2 = n;
while(n1--)
{
ll temp;
cin >> temp;
shelf1.push_back(temp);
}
while(n2--)
{
ll temp;
cin >> temp;
shelf2.push_back(temp);
}
/*cout << "INITIAL : " << endl;
for(int i = 0; i < n; i++) cout << shelf1[i] << ", ";
cout << endl;
for(int i = 0; i < n; i++) cout << shelf2[i] << ", ";
cout << endl;
*/
//sorting the heights in ascending order
sort(shelf1.begin(), shelf1.end(), greater<>());
sort(shelf2.begin(), shelf2.end(), greater<>());
if (shelf1[n - 1] < shelf2[n -1])
{
temp.assign(shelf2.begin(), shelf2.end());
shelf2.assign(shelf1.begin(), shelf1.end());
shelf1.assign(temp.begin(), temp.end());
}
for (int i = 0; i < k; i++)
{
if (shelf2[0] > shelf1[n - 1])
{
swap(shelf2[0], shelf1[n - 1]);
sort(shelf1.begin(), shelf1.end(), greater<>());
sort(shelf2.begin(), shelf2.end(), greater<>());
}
}
/*cout << "FINAL : " << endl;
for(int i = 0; i < n; i++) cout << shelf1[i] << ", ";
cout << endl;
for(int i = 0; i < n; i++) cout << shelf2[i] << ", ";
cout << endl;*/
ll skew = shelf1[0] + shelf2[0];
cout << skew;
//write code above
return 0;
}