Untitled

mail@pastecode.io avatar
unknown
java
a year ago
2.1 kB
2
Indexable
Never


import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int m = scanner.nextInt();
        int n = scanner.nextInt();

        int[] machineSpeed = new int[m];
        int[] w = new int[m];
        for (int i = 0; i < m; i++) {
            machineSpeed[i] = scanner.nextInt();
            w[i] = machineSpeed[i];
        }

        ArrayList<Integer> tasks = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            tasks.add(scanner.nextInt());
        }

        Comparator<Integer> cmp = Collections.reverseOrder();
        Collections.sort(tasks, cmp);

        HashMap<Integer, ArrayList<Double>> machines = new HashMap<>();
        for (int i = 0; i < m; i++) {
            machines.put(i, new ArrayList<>());
        }

        ArrayList<Double> tmpArray = new ArrayList<>();
        int wMin = Integer.MAX_VALUE;
        double toAdd = 0;
        int counter = 0;
        for (int i = 0; i < n; i++) {
            wMin = Integer.MAX_VALUE;
            for (int j = 0; j < m; j++) {
                if (w[j] < wMin) {
                    wMin = w[j];
                    counter = j;
                }
            }
            tmpArray = machines.get(counter);
            toAdd = (double) tasks.get(i) * machineSpeed[counter];
            tmpArray.add(toAdd);
            machines.put(counter, new ArrayList<>(tmpArray));
            tmpArray.clear();
            w[counter] += machineSpeed[counter];
        }

            int result = 0;
            ArrayList<Integer> resultList = new ArrayList<>();
            for (int i = 0; i < m; i++) {
                tmpArray = machines.get(i);
                result = 0;
                for (int j = 0; j < tmpArray.size(); j++) {
                    result += tmpArray.get(tmpArray.size() - j - 1);
                    resultList.add(result);
                }
            }

            int sum = resultList.stream().mapToInt(Integer::intValue).sum();
            System.out.println(sum);

    }
}