import
java.util.*;
public
class
Solution {
public
static
List<Long> calc(
int
n,
int
m, List<Integer> a, List<Integer> b) {
List<Long> ans =
new
ArrayList<>();
PriorityQueue<Pair<Long, Long>> pq =
new
PriorityQueue<>(Comparator.comparingLong(Pair::getFirst));
for
(
int
i =
0
; i < n; i++) {
pq.offer(
new
Pair<>((
long
) b.get(i), (
long
) i));
}
long
total =
0
;
for
(
int
i =
0
; i < n; i++) {
total += a.get(i);
}
total -= total % m;
long
energy =
0
;
ans.add(total);
while
(total >
0
) {
if
(a.get((
int
) (
long
) pq.peek().getSecond()) <= total) {
energy += pq.peek().getFirst() * a.get((
int
) (
long
) pq.peek().getSecond());
total -= a.get((
int
) (
long
) pq.peek().getSecond());
pq.poll();
}
else
if
(a.get((
int
) (
long
) pq.peek().getSecond()) > total) {
energy += pq.peek().getFirst() * total;
total =
0
;
}
}
ans.add(energy);
return
ans;
}
public
static
void
main(String[] args) {
int
n =
4
;
int
m =
4
;
List<Integer> a = Arrays.asList(
1
,
1
,
2
,
1
);
List<Integer> b = Arrays.asList(
2
,
3
,
4
,
4
);
List<Long> ans = calc(n, m, a, b);
for
(
long
val : ans) {
System.out.print(val +
" "
);
}
}
static
class
Pair<K, V> {
private
final
K first;
private
final
V second;
Pair(K first, V second) {
this
.first = first;
this
.second = second;
}
public
K getFirst() {
return
first;
}
public
V getSecond() {
return
second;
}
}
}