# PROBLEM LINK:

Practice

Contest: Division 1

Contest: Division 2

Contest: Division 3

Contest: Division 4

* Author:* raysh07

*sushil2006*

**Tester:***iceknight1093*

**Editorialist:**# DIFFICULTY:

Simple

# PREREQUISITES:

None

# PROBLEM:

There are N items. The i-th item costs A_i if you buy it on any day other than i, and B_i if you buy it on day i.

You can buy one item every day. Find the minimum total cost of buying all N items.

# EXPLANATION:

The absolute best we can do is to buy item i at a cost of \min(A_i, B_i).

This means:

- If A_i \lt B_i, we want to buy item i on some day
*other*than i, but any of the other N-1 days are equally valid. - if A_i \gt B_i, we want to buy item i on exactly day i.
- If A_i = B_i, it doesn’t matter.

First, let’s buy every item with A_i \gt B_i on its respective day.

This will leave us with some items remaining - suppose there are k such items, indexed i_1, i_2, \ldots, i_k.

Now,

- If k = 0, we’re done: every item has been bought for its minimum possible price.
- If k \gt 1, we can always buy every remaining item i_j on a day other than i_j itself.

For example, buy item i_1 on day i_2, item i_2 on day i_3, …, item i_k on day i_1.

That leaves us with the k = 1 case, meaning there’s exactly one item we want to buy on a different day.

However, if we buy item i_1 on a day d that is different from i_1, item d cannot also be bought on day d - and so needs to be bought on some other day.

Since we’re in the case where it’s optimal to buy every item other than i_1 on its own day, we might as well buy item d on day i_1 and leave everything else alone.

If item i_1 is bought on day d, the total cost is A_{i_1} + A_d, plus the sum of B_i values of all other indices.

Try every d and take the best among them - to quickly find the sum of B_i values of all but these two indices, store the sum of all the B_i and subtract two values from it instead.

Apart from that, we can also choose to just buy item i_1 on day i_1, for a cost of B_{i_1} (and then everything else is also bought for B_i since they don’t need to be moved to a different day anymore).

So,

- If k \neq 1, the answer is simply \sum_{i=1}^N \min(A_i, B_i).
- If k = 1, the answer is the minimum of \sum_{i=1}^N B_i, and the minimum of buying i_1 on day d across all d\neq i which can be found by iterating over d.

# TIME COMPLEXITY:

\mathcal{O}(N) per testcase.

# CODE:

## Author's code (C++)

```
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
void Solve()
{
int n; cin >> n;
vector <int> a(n), b(n);
for (auto &x : a) cin >> x;
for (auto &x : b) cin >> x;
int ans = 0;
for (auto x : b){
ans += x;
}
for (int i = 0; i < n; i++){
a[i] -= b[i];
}
sort(a.begin(), a.end());
int val = 0;
int curr = 0;
int sum = ans;
for (int i = 0; i < n; i++){
val += a[i];
if (i){
ans = min(ans, sum + val);
}
}
cout << ans << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
```

## Editorialist's code (PyPy3)

```
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
bad = []
for i in range(n):
if b[i] > a[i]: bad.append(i)
if len(bad) != 1:
ans = sum(min(a[i], b[i]) for i in range(n))
else:
ans = init = sum(b)
i = bad[0]
for j in range(n):
if i == j: continue
ans = min(ans, init - b[i] - b[j] + a[i] + a[j])
print(ans)
```