import
java.util.*;
import
java.io.*;
public
class
GFG{
public
static
void
printSubsets(
int
N,
int
A[],
int
B[]){
Pair v[] =
new
Pair[N];
for
(
int
i =
0
; i < N ; i++){
v[i] =
new
Pair(A[i], i);
}
Arrays.sort(v,
new
comp());
int
ans[] =
new
int
[N];
TreeMap<Integer, Integer> s =
new
TreeMap<Integer,Integer>();
for
(
int
i =
0
; i < N ; i++) {
int
curr_B = B[v[i].second];
int
size =
0
;
int
sum =
0
;
for
(Map.Entry<Integer, Integer> x : s.entrySet()) {
for
(
int
j=
1
; j<=x.getValue() ; j++){
sum += (-x.getKey());
size +=
1
;
if
(sum > curr_B)
break
;
}
if
(sum > curr_B)
break
;
}
if
(s.containsKey(-curr_B)){
s.put(-curr_B, s.get(-curr_B)+
1
);
}
else
{
s.put(-curr_B,
1
);
}
if
(sum > curr_B) {
ans[v[i].second] = size;
}
else
{
ans[v[i].second] = -
1
;
}
}
for
(
int
i =
0
; i < N; i++) {
System.out.print(ans[i] +
" "
);
}
}
public
static
void
main(String args[])
{
int
N =
5
;
int
A[] = {
3
,
2
,
100
,
4
,
5
};
int
B[] = {
1
,
2
,
4
,
3
,
5
};
printSubsets(N, A, B);
}
}
class
Pair{
Integer first;
Integer second;
Pair(
int
x,
int
y){
this
.first = x;
this
.second = y;
}
}
class
comp
implements
Comparator<Pair>{
public
int
compare(Pair x, Pair y){
if
(x.first.equals(y.first)){
return
x.second.compareTo(y.second);
}
return
x.first.compareTo(y.first);
}
}