This is the easy version of this problem. The only difference is that you only need to…
- User
This is the easy version of this problem. The only difference is that you only need to output the minimum total cost of operations in this version. You must solve both versions to be able to hack.
You're given an array a of length n , and an array b of length m (bi>bi+1 for all 1≤i<m ). Initially, the value of k is 1 . Your aim is to make the array a empty by performing one of these two operations repeatedly:
Type 1 — If the value of k is less than m and the array a is not empty, you can increase the value of k by 1 . This does not incur any cost. Type 2 — You remove a non-empty prefix of array a , such that its sum does not exceed bk . This incurs a cost of m−k . You need to minimize the total cost of the operations to make array a empty. If it's impossible to do this through any sequence of operations, output −1 . Otherwise, output the minimum total cost of the operations.
Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤1000 ). The description of the test cases follows.
The first line of each test case contains two integers n and m (1≤n,m≤3⋅105 , 1≤n⋅m≤3⋅105 ).
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109 ).
The third line of each test case contains m integers b1,b2,…,bm (1≤bi≤109 ).
It is also guaranteed that bi>bi+1 for all 1≤i<m .
It is guaranteed that the sum of n⋅m over all test cases does not exceed 3⋅105 .
Output For each test case, if it's possible to make a empty, then output the minimum total cost of the operations.
If there is no possible sequence of operations which makes a empty, then output a single integer −1 .
Example InputCopy 5 4 2 9 3 4 3 11 7 1 2 20 19 18 10 2 2 5 2 1 10 3 2 9 9 6 17 9 10 11 2 2 2 2 2 2 2 2 2 2 20 18 16 14 12 10 8 6 4 2 1 1 6 10 32 16 8 4 2 1 OutputCopy 1 -1 2 10 4 Note In the first test case, one optimal sequence of operations which yields a total cost of 1 is as follows:
Perform an operation of type 2 . Choose the prefix to be [9] . This incurs a cost of 1 . Perform an operation of type 1 . The value of k is now 2 . This incurs no cost. Perform an operation of type 2 . Choose the prefix to be [3,4] . This incurs a cost of 0 . Perform an operation of type 2 . Choose the prefix to be [3] . This incurs a cost of 0 . The array a is now empty, and the total cost of all operations is 1 . In the second test case, it's impossible to remove any prefix of the array since a1>b1 , so array a cannot be made empty by any sequence of operations.
type in c++.Please be quite short code
- Assistant
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while(t--){ int n, m; cin >> n >> m; vector<ll> a(n); for(auto &x: a) cin >> x; vector<ll> b(m); for(auto &x: b) cin >> x; // Check if any a[i] > b[0] bool ok = true; ll max_a = *max_element(a.begin(), a.end()); if(max_a > b[0]){ cout << "-1\n"; continue; } // Compute prefix sums vector<ll> prefix(n+1, 0); for(int i=1;i<=n;i++) prefix[i] = prefix[i-1] + a[i-1]; // Initialize dp // To save space, use two variables // But since we need dp[j], use a dp array vector<ll> dp(n+1, 1e18); dp[0] = 0; for(int i=1;i<=n;i++){ // Iterate over k from1 tom for(int k=1;k<=m;k++){ ll s = prefix[i] - b[k-1]; // Find the smallest j where prefix[j] >= s // j in [0, i] // since prefix is non-decreasing int j = lower_bound(prefix.begin(), prefix.begin()+i, s) - prefix.begin(); if(j > i) continue; // Update dp[i] dp[i] = min(dp[i], dp[j] + (m - (ll)k)); } } if(dp[n] >= 1e18){ cout << "-1\n"; } else{ cout << dp[n] << "\n"; } } }