Problem
You will be given two arrays and , each of length . For some value , let's call an array good if it satisfies the following conditions:
- for
- At least elements in the array must be equal to .
Among all the good arrays , let denote the minimum value of . If no good array is possible, .
Find the value for all .
Input Format
- The first line of input contains an integer denoting the number of test cases. test cases follow.
- The first line of each test case contains a single integer .
- The second line contains integers , the array .
- The third line contains integers , the array .
Output Format
For each test case, output space-separated integers, for .
Constraints
- The sum of over all test cases won't exceed .
Sample 1:
2 5 6 5 2 1 7 10 1 5 1 5 3 7 7 9 8 9 9
12 14 -1 -1 -1 0 -1 -1
Explanation:
Test case :
- is a
goodarray with at least zeroes and gives the minimum value for . - is a
goodarray with at least zeroes and gives the minimum value for . - No good array is possible for the given constraints when or .
Test case :
- is a good array with at least zeroes and gives the minimum value of .
- No good array is possible when or .
#include "bits/stdc++.h"
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
using ll = long long int;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
int t; cin >> t;
while (t--) {
int n; cin >> n;
vector<int> a(n), b(n);
for (int &x : a) cin >> x;
for (int &x : b) cin >> x;
int sumA = accumulate(begin(a), end(a), 0);
int sumB = accumulate(begin(b), end(b), 0);
const int inf = 1e9 + 7;
vector dp(n+1, vector(sumB + 1, inf));
vector<int> ans(n, inf);
dp[0][0] = 0;
for (int i = 0; i < n; ++i) {
for (int ch = i+1; ch >= 1; --ch) {
for (int j = b[i]; j <= sumB; ++j) {
dp[ch][j] = min(dp[ch][j], dp[ch-1][j-b[i]] + max(0, a[i] - b[i]) - a[i]);
if (j >= sumA) ans[n-ch] = min(ans[n-ch], 2*dp[ch][j]);
}
}
}
for (int i = 0; i < n; ++i) {
if (ans[i] < 1e8) cout << ans[i] + 2*sumA << ' ';
else cout << -1 << ' ';
}
cout << '\n';
}
}

0 Comments