import java.io.*;
import java.lang.*;
import java.util.*;

public class Solver {
    public static void main(String[] args) {
        FastReader in = new FastReader();
        PrintWriter out = new PrintWriter(System.out);
        TaskC solver = new TaskC(in, out);
        solver.solveC();
        out.close();
    }

    static class TaskC {
        FastReader in;
        PrintWriter out;

        public TaskC(FastReader in, PrintWriter out) {
            this.in = in;
            this.out = out;
        }

        public void solveA() {
            int t = in.nextInt();
            while (t-- > 0) {
                int n = in.nextInt();
                int[] arrayA = in.nextIntArray(n);
                int[] arrayB = in.nextIntArray(n);

                boolean start = false;
                boolean end = false;
                boolean possible = true;
                int diff = Integer.MIN_VALUE;

                for (int i = 0; i < n; i++) {
                    if (arrayA[i] != arrayB[i]) {
                        if (start && end) {
                            possible = false;
                            break;
                        }
                        int diffA = arrayB[i]-arrayA[i];
                        if (diffA < 0) {
                            possible = false;
                            break;
                        } else {
                            if (diff == Integer.MIN_VALUE) {
                                diff = diffA;
                                start = true;
                            } else {
                                if (diff != diffA) {
                                    possible = false;
                                    break;
                                }
                            }
                        }
                    } else if (start && !end) {
                        end = true;
                    }
                }

                out.println(possible ? "YES" : "NO");
            }
        }

        public void solveB() {
            int n = in.nextInt();
            int[] array = in.nextIntArray(n);

            int days = 0;
            int counter = 0;
            int eventCounter = 0;
            ArrayList<Integer> eventPerDay = new ArrayList<>();

            HashMap<Integer, Integer> map = new HashMap<>();

            for (int i : array) {
                eventCounter++;
                if (i > 0) {
                    if (map.containsKey(i)) {
                        days = -1;
                        break;
                    } else {
                        map.put(i, i);
                        counter += i;
                    }
                } else {
                    int posI = -1 * i;
                    if (!map.containsKey(posI) || map.get(posI) == 0) {
                        days = -1;
                        break;
                    } else {
                        counter -= posI;
                        if (counter == 0) {
                            eventPerDay.add(eventCounter);
                            days++;
                            map = new HashMap<>();
                            eventCounter = 0;
                        }
                    }
                }
            }

            if (days == -1 || counter != 0) {
                out.println(-1);
            } else {
                out.println(days);
                for (int i : eventPerDay) {
                    out.print(i + " ");
                }
            }
        }

        public void solveC() {
            int n = in.nextInt();
            int m = in.nextInt();
            long[] array = in.nextLongArray(n);
            Arrays.sort(array);
            long[] ans = new long[n];

            for (int i = 0; i < n; i++) {
                ans[i] = array[i];
                if (i-m >= 0) {
                    ans[i] += ans[i-m];
                }
            }
            long sum = 0;
            for (int i = 0; i < n; ++i) {
                sum += ans[i];
                out.print(sum + " ");
            }

//            for (int k = 1; k <= n; k++) {
//                long day = 1;
//                long sugar = 0;
//
//                int j = k;
//                for (; j-m >= 0; j=j-m, day++) {
//                    sugar += (day * ( arrayB[j] - arrayB[j-m] ));
//                }
//
//                if (j > 0) {
//                    sugar += (day * (arrayB[j] - arrayB[0]));
//                }
//
//                out.print(sugar + " ");
//            }
        }
    }

    private static long gcd(long a, long b) {
        if (a == 0) {
            return b;
        }
        return gcd(b % a, a);
    }

    private static long lcm(long a, long b) {
        return (a * b) / gcd(a, b);
    }

    private static int min(int a, int b) {
        return a < b ? a : b;
    }

    private static int max(int a, int b) {
        return a > b ? a : b;
    }

    private static int min(ArrayList<Integer> list) {
        int min = Integer.MAX_VALUE;
        for (int el : list) {
            if (el < min) {
                min = el;
            }
        }
        return min;
    }

    private static int max(ArrayList<Integer> list) {
        int max = Integer.MIN_VALUE;
        for (int el : list) {
            if (el > max) {
                max = el;
            }
        }
        return max;
    }

    private static int min(int[] list) {
        int min = Integer.MAX_VALUE;
        for (int el : list) {
            if (el < min) {
                min = el;
            }
        }
        return min;
    }

    private static int max(int[] list) {
        int max = Integer.MIN_VALUE;
        for (int el : list) {
            if (el > max) {
                max = el;
            }
        }
        return max;
    }

    private static void fill(int[] array, int value) {
        for (int i = 0; i < array.length; i++) {
            array[i] = value;
        }
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] nextIntArray(int n) {
            int[] array = new int[n];
            for (int i = 0; i < n; i++) {
                array[i] = nextInt();
            }
            return array;
        }

        long[] nextLongArray(int n) {
            long[] array = new long[n];
            for (int i = 0; i < n; i++) {
                array[i] = nextLong();
            }
            return array;
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

public class que3 {

	public static void main(String[] args) throws IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String []a=br.readLine().split(" ");
  		int n = Integer.parseInt(a[0]);
		int m = Integer.parseInt(a[1]);
		int[] arr = new int[n];
		String []s2=br.readLine().split(" ");
		long[] pre = new long[n];
		for (int i = 0; i < n; i++) {
			arr[i] = Integer.parseInt(s2[i]);

		}
		Arrays.sort(arr);
		pre[0] = arr[0];
		for (int i = 1; i < n; i++) {
			if(i>=m){
				pre[i] += pre[i - m] + arr[i];
			}else{
				pre[i]=arr[i];
			}

		}

		ArrayList<Long> ans = new ArrayList<>();
		long sum=0;
		for (int i = 0; i < n; i++) {
		sum+=arr[i];

			if(i-m>=0){
		  sum+=pre[i-m];	}
			ans.add(sum);

		}
		for (int i = 0; i < arr.length; i++) {
			System.out.print(ans.get(i)+" ");
		}

	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

public class que3 {

	public static void main(String[] args) throws IOException {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		String []a=br.readLine().split(" ");
  		int n = Integer.parseInt(a[0]);
		int m = Integer.parseInt(a[1]);
		int[] arr = new int[n];
		String []s2=br.readLine().split(" ");
		long[] pre = new long[n];
		for (int i = 0; i < n; i++) {
			arr[i] = Integer.parseInt(s2[i]);

		}
		Arrays.sort(arr);
		pre[0] = arr[0];
		for (int i = 1; i < n; i++) {
			if(i>=m){
				pre[i] += pre[i - m] + arr[i];
			}else{
				pre[i]=arr[i];
			}

		}

		ArrayList<Long> ans = new ArrayList<>();
		long sum=0;
		for (int i = 0; i < n; i++) {
		sum+=arr[i];

			if(i-m>0){
		  sum+=pre[i-m];	}
			ans.add(sum);

		}
		for (int i = 0; i < arr.length; i++) {
			System.out.print(ans.get(i)+" ");
		}

	}

}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class que3 {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int m = scn.nextInt();
		int[] arr = new int[n];
		long[] pre = new long[n];
		for (int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();

		}
		Arrays.sort(arr);
		pre[0] = arr[0];
		for (int i = 1; i < n; i++) {
			pre[i] += pre[i - 1] + arr[i];
		}

		ArrayList<Long> ans = new ArrayList<>();
		long [][]dp=new long[arr.length][1000];
		for (int i = 0; i < n; i++) {
			int penalty = 0;
			int l = i;
			long sum = 0;
			int x = 1;

			while (l >= 0) {
				if(dp[l][x]!=0){
					sum+=dp[l][x];
					break;
				}
				sum += (pre[l] - ((l - m) >= 0 ? pre[l - m] : 0)) * x;
                  dp[l][x]=sum;
				x++;
				l -= m;
			}

			ans.add(sum);

		}
		for (int i = 0; i < arr.length; i++) {
			System.out.print(ans.get(i)+" ");
		}

	}

}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class que3 {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int m = scn.nextInt();
		int[] arr = new int[n];
		long[] pre = new long[n];
		for (int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();

		}
		Arrays.sort(arr);
		pre[0] = arr[0];
		for (int i = 1; i < n; i++) {
			pre[i] += pre[i - 1] + arr[i];
		}

		ArrayList<Long> ans = new ArrayList<>();
		long [][]dp=new long[arr.length][100];
		for (int i = 0; i < n; i++) {
			int penalty = 0;
			int l = i;
			long sum = 0;
			int x = 1;

			while (l >= 0) {
				if(dp[l][x]!=0){
					sum+=dp[l][x];
					break;
				}
				sum += (pre[l] - ((l - m) >= 0 ? pre[l - m] : 0)) * x;
                  dp[l][x]=sum;
				x++;
				l -= m;
			}

			ans.add(sum);

		}
		for (int i = 0; i < arr.length; i++) {
			System.out.print(ans.get(i)+" ");
		}

	}

}


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class que3 {

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int m = scn.nextInt();
		int[] arr = new int[n];
		long[] pre = new long[n];
		for (int i = 0; i < n; i++) {
			arr[i] = scn.nextInt();

		}
		Arrays.sort(arr);
		pre[0] = arr[0];
		for (int i = 1; i < n; i++) {
			pre[i] += pre[i - 1] + arr[i];
		}

		ArrayList<Long> ans = new ArrayList<>();
		for (int i = 0; i < n; i++) {
			int penalty = 0;
			int l = i;
			long sum = 0;
			int x = 1;

			while (l >= 0) {
				sum += (pre[l] - ((l - m) >= 0 ? pre[l - m] : 0)) * x;

				x++;
				l -= m;
			}
			ans.add(sum);

		}
		for (int i = 0; i < arr.length; i++) {
			System.out.print(ans.get(i)+" ");
		}

	}

}


import java.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.util.Scanner;
import java.util.StringTokenizer;

public class temp {

	void solve() throws IOException {
		FastReader sc = new FastReader();
		int n = sc.nextInt();
		int m = sc.nextInt();
		long a[] = new long[n];
		for(int i=0;i<n;i++)
			a[i] = sc.nextLong();

		Arrays.sort(a);
		long sum = 0;
		for(int i=0;i<n;i++)
		{
			if(i>=m)
                a[i] += a[i-m];
            sum += a[i];
            System.out.print(sum+" ");
		}

	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		new temp().solve();
	}

	class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}
}


import java.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.util.Scanner;
import java.util.StringTokenizer;

public class temp {

	void solve() throws IOException {
		FastReader sc = new FastReader();
		int n = sc.nextInt();
		int m = sc.nextInt();
		int a[] = new int[n];
		for(int i=0;i<n;i++)
			a[i] = sc.nextInt();

		Arrays.sort(a);

		int sum[] = new int[n];
		sum[0] = a[0];
		for(int i=1;i<n;i++)
			sum[i] = sum[i-1] + a[i];

		int ans[] = new int[n];
		for(int i=0;i<n;i++)
		{
			ans[i] = sum[i];
			for(int j=i-m;j>=0;j-=m)
				ans[i]+=sum[j];
		}

		for(int i=0;i<n;i++)
			System.out.print(ans[i]+" ");
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		new temp().solve();
	}

	class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}
}


import java.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.util.Scanner;
import java.util.StringTokenizer;

public class temp {

	void solve() throws IOException {
		FastReader sc = new FastReader();
		int n = sc.nextInt();
		int m = sc.nextInt();
		long a[] = new long[n];
		for(int i=0;i<n;i++)
			a[i] = sc.nextLong();

		Arrays.sort(a);

		long sum[] = new long[n];
		sum[0] = a[0];
		for(int i=1;i<n;i++)
			sum[i] = sum[i-1] + a[i];

		long ans[] = new long[n];
		for(int i=0;i<n;i++)
		{
			ans[i] = sum[i];
			for(int j=i-m;j>=0;j-=m)
				ans[i]+=sum[j];
		}

		for(int i=0;i<n;i++)
			System.out.print(ans[i]+" ");
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		new temp().solve();
	}

	class FastReader {
		BufferedReader br;
		StringTokenizer st;

		public FastReader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}
}


import java.util.*;
import java.awt.Point;
import java.io.*;

public class Main{
    public static void read(long[] a,int n) throws IOException{for (int i=0;i<n;i++){a[i]=Reader.nextLong();}}
    public static String print(int[] a,int n,String c){String A=""; for (int i : a){ A+=i+c;} return A;}
    public static String send(String a,String b){ if (a.charAt(0)==b.charAt(0)){ if (a.equals("R")){ return "B";}else if(a.equals("B")){ return "G";}else{ return "R";}}
                                                  else{ if (a.equals("R") && b.equals("B") || a.equals("B") && b.equals("R")){ return "G";}else if (a.equals("R") && b.equals("G") || a.equals("G") && b.equals("R")){ return "B";}else { return "R";}}
                                                }

    public static void main(String[] args) throws IOException{
        Reader scan = new Reader();
        scan.init(System.in);
        OutputStream output = System.out;
        PrintWriter out = new PrintWriter(output);
        int n = scan.nextInt();
        int m = scan.nextInt();
        int[] arr = new int[n];
        for (int i=0;i<n;i++){
            arr[i] = scan.nextInt();
        }
        Arrays.sort(arr);
        for (int i=1;i<n;i++){
            arr[i] = arr[i-1]+arr[i];
        }
        for (int i=m;i<n;i++){
            arr[i] += arr[i-m];
        }
        for (int i : arr){
            out.print(i+" ");
        }
        out.println();
        out.close();
    }
}
class Comp implements Comparator<Point>{
    public int compare(Point a,Point b){
        return (a.x-b.x);
    }
}
class dsu{
    int[] arr;
    dsu(int N){
        arr = new int[N];
        for (int i=0;i<N;i++){
            arr[i] = i;
        }
    }
    public void joint(int A,int B){
        int a = find(A);
        int b = find(B);
        arr[a] = b;
    }
    public int find(int A){
        while(A!=arr[A]){
            A = arr[A];
        }
        return A;
    }
}
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}


import java.util.*;
import java.awt.Point;
import java.io.*;

public class Main{
    public static void read(long[] a,int n) throws IOException{for (int i=0;i<n;i++){a[i]=Reader.nextLong();}}
    public static String print(int[] a,int n,String c){String A=""; for (int i : a){ A+=i+c;} return A;}
    public static String send(String a,String b){ if (a.charAt(0)==b.charAt(0)){ if (a.equals("R")){ return "B";}else if(a.equals("B")){ return "G";}else{ return "R";}}
                                                  else{ if (a.equals("R") && b.equals("B") || a.equals("B") && b.equals("R")){ return "G";}else if (a.equals("R") && b.equals("G") || a.equals("G") && b.equals("R")){ return "B";}else { return "R";}}
                                                }

    public static void main(String[] args) throws IOException{
        Reader scan = new Reader();
        scan.init(System.in);
        OutputStream output = System.out;
        PrintWriter out = new PrintWriter(output);
        int n = scan.nextInt();
        int m = scan.nextInt();
        long[] arr = new long[n];
        for (int i=0;i<n;i++){
            arr[i] = scan.nextInt();
        }
        Arrays.sort(arr);
        for (int i=1;i<n;i++){
            arr[i] = arr[i-1]+arr[i];
        }
        for (int i=m;i<n;i++){
            arr[i] += arr[i-m];
        }
        for (long i : arr){
            out.print(i+" ");
        }
        out.println();
        out.close();
    }
}
class Comp implements Comparator<Point>{
    public int compare(Point a,Point b){
        return (a.x-b.x);
    }
}
class dsu{
    int[] arr;
    dsu(int N){
        arr = new int[N];
        for (int i=0;i<N;i++){
            arr[i] = i;
        }
    }
    public void joint(int A,int B){
        int a = find(A);
        int b = find(B);
        arr[a] = b;
    }
    public int find(int A){
        while(A!=arr[A]){
            A = arr[A];
        }
        return A;
    }
}
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main
{
	InputStream is;
	PrintWriter out;
	String INPUT = "";

	void solve()
	{
		int n = ni(), m = ni();
		int[] a = na(n);
		Arrays.sort(a);
		int[] index = new int[n];
		long[] cum = new long[m];
		for(int i = 0; i < m; i++) {
			for(int j = i; j < n; j += m) {
				index[j] = i;
				cum[i] += a[j];
			}
		}

		long tot = 0;
		int ct = 0, mul = 1;
		for(int i = n-1; i >= 0; i--) {
			tot += a[i]  * mul;
			ct++;
			if(ct == m) {
				ct = 0;
				mul++;
			}
		}
		long[] ans = new long[n];
		for(int i = n-1; i >= 0; i--) {
			if(i+1+m < n) {
				cum[index[i+1]] -= a[i+1+m];
			}
			if(i == n-1) {
				ans[i] = tot;
			}
			else {
				tot -= cum[index[i+1]];
				ans[i] = tot;
			}

		}
		for(long v : ans) {
			out.print(v+" ");
		}
		out.println();

	}

	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
	}

	public static void main(String[] args) throws Exception
	{ new Main().run(); }

	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;

	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf)
		{
			ptrbuf = 0;
			try
			{ lenbuf = is.read(inbuf); }
			catch (IOException e)
			{ throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}

	private boolean isSpaceChar(int c)
	{ return !(c >= 33 && c <= 126); }
	private int skip()
	{ int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

	private double nd()
	{ return Double.parseDouble(ns()); }
	private char nc()
	{ return (char)skip(); }

	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b)))
		{ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b)))
		{
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}

	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}

	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}

	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-')
		{
			minus = true;
			b = readByte();
		}

		while(true)
		{
			if(b >= '0' && b <= '9')
			{
				num = num * 10 + (b - '0');
			}else
			{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-')
		{
			minus = true;
			b = readByte();
		}

		while(true){
			if(b >= '0' && b <= '9')
			{
				num = num * 10 + (b - '0');
			}else
			{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private static void tr(Object... o)
	{ System.out.println(Arrays.deepToString(o)); }
}


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main
{
	InputStream is;
	PrintWriter out;
	String INPUT = "";

	void solve()
	{
		int n = ni(), m = ni();
		int[] a = na(n);
		Arrays.sort(a);
		int[] index = new int[n];
		long[] cum = new long[m];
		for(int i = 0; i < m; i++) {
			for(int j = i; j < n; j += m) {
				index[j] = i;
				cum[i] += a[j];
			}
		}

		long tot = 0;
		int ct = 0, mul = 1;
		for(int i = n-1; i >= 0; i--) {
			tot += (long)a[i]  * mul;
			ct++;
			if(ct == m) {
				ct = 0;
				mul++;
			}
		}
		long[] ans = new long[n];
		for(int i = n-1; i >= 0; i--) {
			if(i+1+m < n) {
				cum[index[i+1]] -= a[i+1+m];
			}
			if(i == n-1) {
				ans[i] = tot;
			}
			else {
				tot -= cum[index[i+1]];
				ans[i] = tot;
			}

		}
		for(long v : ans) {
			out.print(v+" ");
		}
		out.println();

	}

	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
	}

	public static void main(String[] args) throws Exception
	{ new Main().run(); }

	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;

	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf)
		{
			ptrbuf = 0;
			try
			{ lenbuf = is.read(inbuf); }
			catch (IOException e)
			{ throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}

	private boolean isSpaceChar(int c)
	{ return !(c >= 33 && c <= 126); }
	private int skip()
	{ int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }

	private double nd()
	{ return Double.parseDouble(ns()); }
	private char nc()
	{ return (char)skip(); }

	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b)))
		{ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b)))
		{
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}

	private char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}

	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}

	private int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-')
		{
			minus = true;
			b = readByte();
		}

		while(true)
		{
			if(b >= '0' && b <= '9')
			{
				num = num * 10 + (b - '0');
			}else
			{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-')
		{
			minus = true;
			b = readByte();
		}

		while(true){
			if(b >= '0' && b <= '9')
			{
				num = num * 10 + (b - '0');
			}else
			{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private static void tr(Object... o)
	{ System.out.println(Arrays.deepToString(o)); }
}


import javafx.util.Pair;

import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.Map.Entry;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.InputMismatchException;

public class abc {

	static String[] temp= {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
	static char[] ans;

		public static void main(String[] args) throws Exception {
			// TODO Auto-generated method stub
			InputReader s = new InputReader(System.in);
			PrintWriter p = new PrintWriter(System.out);
			int n=s.nextInt();
			int k=s.nextInt();
			int[] arr=new int[n];
			for(int i=0 ; i<arr.length ; i++) {
				arr[i]=s.nextInt();
			}
			Arrays.sort(arr);
			int sum=0;
			int[] temp=new int[k];
			for(int i=0 ; i<arr.length ; i++) {
				sum+=arr[i];
				sum+=temp[i%k];
				temp[i%k]+=arr[i];
				System.out.print(sum+" ");
			}
			System.out.println();
		}
		public class Pair implements Comparable<Pair>{
	        int x;
	        int f;
	        public Pair(int a , int b){
	            x=a;
	            f=b;
	        }

	        public int comprae(Pair p){
	            return p.f-this.f;
	        }

			@Override
			public int compareTo(Pair arg0) {
				// TODO Auto-generated method stub
				return 0;
			}
	    }

		public static int helper(String[] arr , int pos , int k , int[][] dp) {
			if(pos==arr.length && k==0) {
				return 1;
			}
			if(pos==arr.length || k==0) return 0;
			if(dp[pos][k]!=-1) return dp[pos][k];
			for(int i=9 ; i>=0 ; i--) {
				String s1=temp[i];
				String curr=arr[pos];
				int con=convert(curr,s1);
				if(con==-1 || con>k) {
					continue;
				}
				ans[pos]=(char)(i+'0');
				int z=helper(arr,pos+1,k-con,dp);
				if(z==1) {
					dp[pos][k]=1;
					return 1;
				}
				ans[pos]='z';
			}
			return dp[pos][k]=0;
		}

		public static int convert(String curr , String s1) {
			int count=0;
			for(int i=0 ; i<7 ; i++) {
				char c1=curr.charAt(i);
				char c2=s1.charAt(i);
				if(c1=='1' && c2=='0') {
					return -1;
				}
				if(c1=='0' && c2=='1') {
					count++;
				}
			}
			return count;
		}

		static long power(long x, long y)
	    {
			long temp=0l;
	        if( y == 0)
	            return 1l;
	        temp = power(x, y/2);

	        if (y%2 == 0)
	            return temp*temp;
	        else
	        {
	            if(y > 0)
	                return x * temp * temp;
	            else
	                return (temp * temp) / x;
	        }
	    }

		public static boolean helper(int[] arr1 , int[] arr2) {
			boolean[] temp=new boolean[3];
			if(arr1.length==0) return true;
			if(arr1[0]!=arr2[0]) return false;
			temp[arr1[0]+1]=true;
			for(int i=1 ; i<arr1.length ; i++) {

				if(arr1[i]==arr2[i]) {

				}else if(arr1[i]<arr2[i]) {
					if(!temp[2]) {
						return false;
					}
				}else {
					if(!temp[0]) {
						return false;
					}
				}
				temp[arr1[i]+1]=true;
			}
			return true;
		}

		public static ArrayList Divisors(long n) {
			ArrayList<Long> div = new ArrayList<>();

			for (long i = 1; i <= Math.sqrt(n); i++) {
				if (n % i == 0) {
					div.add(i);

					if (n / i != i)
						div.add(n / i);
				}
			}
			return div;
		}

		public static int BinarySearch(long[] a, long k) {
			int n = a.length;
			int i = 0, j = n - 1;
			int mid = 0;

			if (k < a[0])
				return 0;
			else if (k >= a[n - 1])
				return n;
			else {
				while (j - i > 1) {
					mid = (i + j) / 2;

					if (k >= a[mid])
						i = mid;
					else
						j = mid;
				}
			}

			return i + 1;
		}

		public static long GCD(long a, long b) {
			if (b == 0)
				return a;
			else
				return GCD(b, a % b);
		}

		public static long LCM(long a, long b) {
			return (a * b) / GCD(a, b);
		}

		static class InputReader {

			private final InputStream stream;
			private final byte[] buf = new byte[8192];
			private int curChar, snumChars;
			private SpaceCharFilter filter;

			public InputReader(InputStream stream) {
				this.stream = stream;
			}

			public int snext() {
				if (snumChars == -1)
					throw new InputMismatchException();
				if (curChar >= snumChars) {
					curChar = 0;
					try {
						snumChars = stream.read(buf);
					} catch (IOException e) {
						throw new InputMismatchException();
					}
					if (snumChars <= 0)
						return -1;
				}
				return buf[curChar++];
			}

			static class pair implements Comparable<pair> {
				Integer x, y;

				pair(int x, int y) {
					this.x = x;
					this.y = y;
				}

				public int compareTo(pair o) {
					int result = x.compareTo(o.x);
					if (result == 0)
						result = y.compareTo(o.y);

					return result;
				}

				public String toString() {
					return x + " " + y;
				}

				public boolean equals(Object o) {
					if (o instanceof pair) {
						pair p = (pair) o;
						return p.x - x == 0 && p.y - y == 0;
					}
					return false;
				}

				public int hashCode() {
					return new Long(x).hashCode() * 31 + new Long(y).hashCode();
				}
			}

			public int nextInt() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				int sgn = 1;
				if (c == '-') {
					sgn = -1;
					c = snext();
				}
				int res = 0;
				do {
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					res *= 10;
					res += c - '0';
					c = snext();
				} while (!isSpaceChar(c));
				return res * sgn;
			}

			public long nextLong() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				int sgn = 1;
				if (c == '-') {
					sgn = -1;
					c = snext();
				}
				long res = 0;
				do {
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					res *= 10;
					res += c - '0';
					c = snext();
				} while (!isSpaceChar(c));
				return res * sgn;
			}

			public int[] nextIntArray(int n) {
				int a[] = new int[n];
				for (int i = 0; i < n; i++) {
					a[i] = nextInt();
				}
				return a;
			}

			public String readString() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				StringBuilder res = new StringBuilder();
				do {
					res.appendCodePoint(c);
					c = snext();
				} while (!isSpaceChar(c));
				return res.toString();
			}

			public String nextLine() {
				int c = snext();
				while (isSpaceChar(c))
					c = snext();
				StringBuilder res = new StringBuilder();
				do {
					res.appendCodePoint(c);
					c = snext();
				} while (!isEndOfLine(c));
				return res.toString();
			}

			public boolean isSpaceChar(int c) {
				if (filter != null)
					return filter.isSpaceChar(c);
				return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
			}

			private boolean isEndOfLine(int c) {
				return c == '\n' || c == '\r' || c == -1;
			}

			public interface SpaceCharFilter {
				public boolean isSpaceChar(int ch);
			}
		}

		static class CodeX {
			public static void sort(long arr[]) {
				merge_sort(arr, 0, arr.length - 1);
			}

			private static void merge_sort(long A[], long start, long end) {
				if (start < end) {
					long mid = (start + end) / 2;
					merge_sort(A, start, mid);
					merge_sort(A, mid + 1, end);
					merge(A, start, mid, end);
				}

			}

			private static void merge(long A[], long start, long mid, long end) {
				long p = start, q = mid + 1;
				long Arr[] = new long[(int) (end - start + 1)];
				long k = 0;

				for (int i = (int) start; i <= end; i++) {
					if (p > mid)
						Arr[(int) k++] = A[(int) q++];

					else if (q > end)
						Arr[(int) k++] = A[(int) p++];

					else if (A[(int) p] < A[(int) q])
						Arr[(int) k++] = A[(int) p++];

					else
						Arr[(int) k++] = A[(int) q++];
				}
				for (int i = 0; i < k; i++) {
					A[(int) start++] = Arr[i];
				}

			}

		}

	}


import javafx.util.Pair;

import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.*;
import java.util.Map.Entry;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.InputMismatchException;

public class abc {

	static String[] temp= {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"};
	static char[] ans;

		public static void main(String[] args) throws Exception {
			// TODO Auto-generated method stub
			InputReader s = new InputReader(System.in);
			PrintWriter p = new PrintWriter(System.out);
			int n=s.nextInt();
			int k=s.nextInt();
			long[] arr=new long[n];
			for(int i=0 ; i<arr.length ; i++) {
				arr[i]=s.nextInt();
			}
			Arrays.sort(arr);
			long sum=0;
			long[] temp=new long[k];
			for(int i=0 ; i<arr.length ; i++) {
				sum+=arr[i];
				sum+=temp[i%k];
				temp[i%k]+=arr[i];
				System.out.print(sum+" ");
			}
			System.out.println();
		}
		public class Pair implements Comparable<Pair>{
	        int x;
	        int f;
	        public Pair(int a , int b){
	            x=a;
	            f=b;
	        }

	        public int comprae(Pair p){
	            return p.f-this.f;
	        }

			@Override
			public int compareTo(Pair arg0) {
				// TODO Auto-generated method stub
				return 0;
			}
	    }

		public static int helper(String[] arr , int pos , int k , int[][] dp) {
			if(pos==arr.length && k==0) {
				return 1;
			}
			if(pos==arr.length || k==0) return 0;
			if(dp[pos][k]!=-1) return dp[pos][k];
			for(int i=9 ; i>=0 ; i--) {
				String s1=temp[i];
				String curr=arr[pos];
				int con=convert(curr,s1);
				if(con==-1 || con>k) {
					continue;
				}
				ans[pos]=(char)(i+'0');
				int z=helper(arr,pos+1,k-con,dp);
				if(z==1) {
					dp[pos][k]=1;
					return 1;
				}
				ans[pos]='z';
			}
			return dp[pos][k]=0;
		}

		public static int convert(String curr , String s1) {
			int count=0;
			for(int i=0 ; i<7 ; i++) {
				char c1=curr.charAt(i);
				char c2=s1.charAt(i);
				if(c1=='1' && c2=='0') {
					return -1;
				}
				if(c1=='0' && c2=='1') {
					count++;
				}
			}
			return count;
		}

		static long power(long x, long y)
	    {
			long temp=0l;
	        if( y == 0)
	            return 1l;
	        temp = power(x, y/2);

	        if (y%2 == 0)
	            return temp*temp;
	        else
	        {
	            if(y > 0)
	                return x * temp * temp;
	            else
	                return (temp * temp) / x;
	        }
	    }

		public static boolean helper(int[] arr1 , int[] arr2) {
			boolean[] temp=new boolean[3];
			if(arr1.length==0) return true;
			if(arr1[0]!=arr2[0]) return false;
			temp[arr1[0]+1]=true;
			for(int i=1 ; i<arr1.length ; i++) {

				if(arr1[i]==arr2[i]) {

				}else if(arr1[i]<arr2[i]) {
					if(!temp[2]) {
						return false;
					}
				}else {
					if(!temp[0]) {
						return false;
					}
				}
				temp[arr1[i]+1]=true;
			}
			return true;
		}

		public static ArrayList Divisors(long n) {
			ArrayList<Long> div = new ArrayList<>();

			for (long i = 1; i <= Math.sqrt(n); i++) {
				if (n % i == 0) {
					div.add(i);

					if (n / i != i)
						div.add(n / i);
				}
			}
			return div;
		}

		public static int BinarySearch(long[] a, long k) {
			int n = a.length;
			int i = 0, j = n - 1;
			int mid = 0;

			if (k < a[0])
				return 0;
			else if (k >= a[n - 1])
				return n;
			else {
				while (j - i > 1) {
					mid = (i + j) / 2;

					if (k >= a[mid])
						i = mid;
					else
						j = mid;
				}
			}

			return i + 1;
		}

		public static long GCD(long a, long b) {
			if (b == 0)
				return a;
			else
				return GCD(b, a % b);
		}

		public static long LCM(long a, long b) {
			return (a * b) / GCD(a, b);
		}

		static class InputReader {

			private final InputStream stream;
			private final byte[] buf = new byte[8192];
			private int curChar, snumChars;
			private SpaceCharFilter filter;

			public InputReader(InputStream stream) {
				this.stream = stream;
			}

			public int snext() {
				if (snumChars == -1)
					throw new InputMismatchException();
				if (curChar >= snumChars) {
					curChar = 0;
					try {
						snumChars = stream.read(buf);
					} catch (IOException e) {
						throw new InputMismatchException();
					}
					if (snumChars <= 0)
						return -1;
				}
				return buf[curChar++];
			}

			static class pair implements Comparable<pair> {
				Integer x, y;

				pair(int x, int y) {
					this.x = x;
					this.y = y;
				}

				public int compareTo(pair o) {
					int result = x.compareTo(o.x);
					if (result == 0)
						result = y.compareTo(o.y);

					return result;
				}

				public String toString() {
					return x + " " + y;
				}

				public boolean equals(Object o) {
					if (o instanceof pair) {
						pair p = (pair) o;
						return p.x - x == 0 && p.y - y == 0;
					}
					return false;
				}

				public int hashCode() {
					return new Long(x).hashCode() * 31 + new Long(y).hashCode();
				}
			}

			public int nextInt() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				int sgn = 1;
				if (c == '-') {
					sgn = -1;
					c = snext();
				}
				int res = 0;
				do {
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					res *= 10;
					res += c - '0';
					c = snext();
				} while (!isSpaceChar(c));
				return res * sgn;
			}

			public long nextLong() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				int sgn = 1;
				if (c == '-') {
					sgn = -1;
					c = snext();
				}
				long res = 0;
				do {
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					res *= 10;
					res += c - '0';
					c = snext();
				} while (!isSpaceChar(c));
				return res * sgn;
			}

			public int[] nextIntArray(int n) {
				int a[] = new int[n];
				for (int i = 0; i < n; i++) {
					a[i] = nextInt();
				}
				return a;
			}

			public String readString() {
				int c = snext();
				while (isSpaceChar(c)) {
					c = snext();
				}
				StringBuilder res = new StringBuilder();
				do {
					res.appendCodePoint(c);
					c = snext();
				} while (!isSpaceChar(c));
				return res.toString();
			}

			public String nextLine() {
				int c = snext();
				while (isSpaceChar(c))
					c = snext();
				StringBuilder res = new StringBuilder();
				do {
					res.appendCodePoint(c);
					c = snext();
				} while (!isEndOfLine(c));
				return res.toString();
			}

			public boolean isSpaceChar(int c) {
				if (filter != null)
					return filter.isSpaceChar(c);
				return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
			}

			private boolean isEndOfLine(int c) {
				return c == '\n' || c == '\r' || c == -1;
			}

			public interface SpaceCharFilter {
				public boolean isSpaceChar(int ch);
			}
		}

		static class CodeX {
			public static void sort(long arr[]) {
				merge_sort(arr, 0, arr.length - 1);
			}

			private static void merge_sort(long A[], long start, long end) {
				if (start < end) {
					long mid = (start + end) / 2;
					merge_sort(A, start, mid);
					merge_sort(A, mid + 1, end);
					merge(A, start, mid, end);
				}

			}

			private static void merge(long A[], long start, long mid, long end) {
				long p = start, q = mid + 1;
				long Arr[] = new long[(int) (end - start + 1)];
				long k = 0;

				for (int i = (int) start; i <= end; i++) {
					if (p > mid)
						Arr[(int) k++] = A[(int) q++];

					else if (q > end)
						Arr[(int) k++] = A[(int) p++];

					else if (A[(int) p] < A[(int) q])
						Arr[(int) k++] = A[(int) p++];

					else
						Arr[(int) k++] = A[(int) q++];
				}
				for (int i = 0; i < k; i++) {
					A[(int) start++] = Arr[i];
				}

			}

		}

	}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class c600 {
    static PrintWriter out;
    static BufferedReader in;
    static StringTokenizer st;

    public static void main(String[] args) throws FileNotFoundException {

        out = new PrintWriter(System.out);
        in = new BufferedReader(new InputStreamReader(System.in));
        new c600().Run();
        out.close();
    }

    String ns() {
        try {
            if (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(in.readLine());
            }
            return st.nextToken();
        } catch (Exception e) {
            return null;
        }
    }

    int nextint() {
        return Integer.valueOf(ns());
    }

    String nextline() {
        st = null;
        try {
            return in.readLine();
        } catch (Exception e) {
            return null;
        }
    }

    public void Run() {
        int total = nextint();
        int allowedPerDay = nextint();

        int [] v = new int [total + 1];
        long [] d = new long [total + 1];
        long [] res = new long [total +1];

        for(int i = 0; i < total; i++) v[i] = nextint();

        Arrays.sort(v);

        for(int i = 1; i < total + 1; i++){
            d[i] = v[i] + d[i-1];
        }
        for(int i = 1; i < total + 1; i++){
            res[i] = res[Math.max(0, i-allowedPerDay)] + d[i];
            out.print(res[i] + " ");
        }
    }
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class c600 {
    static PrintWriter out;
    static BufferedReader in;
    static StringTokenizer st;

    public static void main(String[] args) throws FileNotFoundException {
        //out = new PrintWriter("test.out");
        //in = new BufferedReader(new FileReader("test.in"));
        out = new PrintWriter(System.out);
        in = new BufferedReader(new InputStreamReader(System.in));
        new c600().Run();
        out.close();
    }

    String ns() {
        try {
            if (st == null || !st.hasMoreTokens()) {
                st = new StringTokenizer(in.readLine());
            }
            return st.nextToken();
        } catch (Exception e) {
            return null;
        }
    }

    int nextint() {
        return Integer.valueOf(ns());
    }

    String nextline() {
        st = null;
        try {
            return in.readLine();
        } catch (Exception e) {
            return null;
        }
    }

    public void Run() {
        int n = nextint();
        int m = nextint();
        long sum = 0;
        int e = 1;

        int [] v = new int [n];
        ArrayList<Integer> d = new ArrayList<>();

        for(int i = 0; i < n; i++){ v[i] = nextint(); }

        Arrays.sort(v);

        int q = m;
        int k = 0;
        while(k++ < n){
            int b = k;
            while (b-- > 0){
                d.add(v[b]);
            }
            while(d.size() > 0){

                sum = sum + d.get(0) * e;
                d.remove(0);
                q--;
                if (q == 0){
                    e++;
                    q=m;
                }
            }

            out.print(sum + " ");
            sum = 0;
            e = 1;
            q = m;
        }
    }
}


import java.util.*;
import java.io.*;

public class b {

	public static void main(String[] args) {

		Scanner s=new Scanner(System.in);

		int n=s.nextInt();
		int m=s.nextInt();
		Long[] arr=new Long[n];
		for(int i=0;i<n;i++) {
			arr[i]=s.nextLong();

		}
		Arrays.sort(arr);
		long[] pre=new long[n];

		pre[0]=arr[0];
		for(int i=1;i<n;i++) {
			pre[i]=pre[i-1]+arr[i];
		}

		long[] ans=new long[n];
		ans[0]=pre[0];
		for(int i=1;i<n;i++) {
			if(i+1<=m) {
				ans[i]=pre[i];
			}else {
				ans[i]=pre[i]+ans[i-m];
			}
		}

		for(int i=0;i<n;i++) {
			System.out.print(ans[i]+" ");
		}

	}
}


import java.util.Arrays;
import java.util.Scanner;

public class b {

	public static void main(String[] args) {

		Scanner s=new Scanner(System.in);
StringBuilder sb=new StringBuilder();
		int n=s.nextInt();
		int m=s.nextInt();
		Long[] arr=new Long[n];
		for(int i=0;i<n;i++) {
			arr[i]=s.nextLong();

		}
		Arrays.sort(arr);
		long[] pre=new long[n];

		pre[0]=arr[0];
		for(int i=1;i<n;i++) {
			pre[i]=pre[i-1]+arr[i];
		}

		long[] ans=new long[n];
		ans[0]=pre[0];
		for(int i=1;i<n;i++) {
			if(i+1<=m) {
				ans[i]=pre[i];
			}else {
				ans[i]=pre[i]+ans[i-m];
			}
		}

		for(int i=0;i<n;i++) {
			sb.append(ans[i]);
			sb.append(" ");
		}
	System.out.println(sb);

	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.stream.IntStream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Collections;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main
{
    public static void main(String[] args)
    {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC
    {
        public void solve(int testNumber, Scanner in, PrintWriter out)
        {
            int n = in.nextInt();
            int m = in.nextInt();
            int sweets[] = CPUtils.readIntArray(n, in);
            int arr[] = sweets;
            sweets = Arrays.stream(sweets).boxed()
                           .sorted(Collections.reverseOrder())
                           .mapToInt(Integer::intValue)
                           .toArray();

            //init
            List<LinkedList<Integer>> days = new ArrayList<>();
            LinkedList<Integer> list = new LinkedList<>();
            int sweetsPerDay = 0;
            int daysN = 1;
            int totalSum = 0;
            for (int i = 0; i < sweets.length; i++)
            {
                if (sweetsPerDay == m)
                {
                    days.add(list);
                    list = new LinkedList<>();
                    sweetsPerDay = 0;
                    daysN++;
                }

                list.add(sweets[i]);
                sweetsPerDay++;
                totalSum += (sweets[i] * daysN);

                if (i == sweets.length - 1)
                {
                    days.add(list);
                }
            }
            List<Integer> result = new ArrayList<>();
            result.add(totalSum);

            for (int i = 0; i < n - 1; i++)
            {

                for (int j = 0; j < days.size() - 1; j++)
                {
                    //remove first from list 1
                    totalSum -= ((j + 1) * days.get(j).getFirst());
                    days.get(j).removeFirst();

                    days.get(j).addLast(days.get(j + 1).getFirst());
                    totalSum += ((j + 1) * days.get(j + 1).getFirst());
                }
                totalSum -= (days.size()) * days.get(days.size() - 1).getFirst();
                days.get(days.size() - 1).removeFirst();

                if (days.get(days.size() - 1).size() == 0)
                    days.remove(days.size() - 1);
                result.add(totalSum);
            }
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = result.size() - 1; i >= 0; i--)
            {
                if (i != 0)
                    stringBuilder.append(result.get(i)).append(" ");
                else
                    stringBuilder.append(result.get(i));
            }
            out.print(stringBuilder.toString());
        }

    }

    static class CPUtils
    {
        public static int[] readIntArray(int size, Scanner in)
        {
            int[] array = new int[size];
            for (int i = 0; i < size; i++)
            {
                array[i] = in.nextInt();
            }
            return array;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.stream.IntStream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Collections;
import java.util.ArrayList;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main
{
    public static void main(String[] args)
    {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC
    {
        public void solve(int testNumber, Scanner in, PrintWriter out)
        {
            int n = in.nextInt();
            int m = in.nextInt();
            int sweets[] = CPUtils.readIntArray(n, in);
            int arr[] = sweets;
            sweets = Arrays.stream(sweets).boxed()
                           .sorted(Collections.reverseOrder())
                           .mapToInt(Integer::intValue)
                           .toArray();

            //init
            List<LinkedList<Integer>> days = new ArrayList<>();
            LinkedList<Integer> list = new LinkedList<>();
            int sweetsPerDay = 0;
            int daysN = 1;
            long totalSum = 0;
            for (int i = 0; i < sweets.length; i++)
            {
                if (sweetsPerDay == m)
                {
                    days.add(list);
                    list = new LinkedList<>();
                    sweetsPerDay = 0;
                    daysN++;
                }

                list.add(sweets[i]);
                sweetsPerDay++;
                totalSum += (sweets[i] * daysN);

                if (i == sweets.length - 1)
                {
                    days.add(list);
                }
            }
            List<Long> result = new ArrayList<>();
            result.add(totalSum);

            for (int i = 0; i < n - 1; i++)
            {

                for (int j = 0; j < days.size() - 1; j++)
                {
                    //remove first from list 1
                    totalSum -= ((j + 1) * days.get(j).getFirst());
                    days.get(j).removeFirst();

                    days.get(j).addLast(days.get(j + 1).getFirst());
                    totalSum += ((j + 1) * days.get(j + 1).getFirst());
                }
                totalSum -= (days.size()) * days.get(days.size() - 1).getFirst();
                days.get(days.size() - 1).removeFirst();

                if (days.get(days.size() - 1).size() == 0)
                    days.remove(days.size() - 1);
                result.add(totalSum);
            }
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = result.size() - 1; i >= 0; i--)
            {
                if (i != 0)
                    stringBuilder.append(result.get(i)).append(" ");
                else
                    stringBuilder.append(result.get(i));
            }
            out.print(stringBuilder.toString());
        }

    }

    static class CPUtils
    {
        public static int[] readIntArray(int size, Scanner in)
        {
            int[] array = new int[size];
            for (int i = 0; i < size; i++)
            {
                array[i] = in.nextInt();
            }
            return array;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main
{
    public static void main(String[] args)
    {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC
    {
        public void solve(int testNumber, Scanner in, PrintWriter out)
        {
            int n = in.nextInt();
            int m = in.nextInt();
            int sweets[] = CPUtils.readIntArray(n, in);
            Arrays.sort(sweets);

            long add[] = new long[m];
            long answer[] = new long[n];
            long current = 0;
            for (int i = 0; i < n; i++)
            {
                add[i % m] += sweets[i];
                current += add[i % m];
                answer[i] = current;
            }
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < answer.length; i++)
            {
                if (i != answer.length - 1)
                    stringBuilder.append(answer[i]).append(" ");
                else
                    stringBuilder.append(answer[i]);

            }
            out.print(stringBuilder.toString());
        }

    }

    static class CPUtils
    {
        public static int[] readIntArray(int size, Scanner in)
        {
            int[] array = new int[size];
            for (int i = 0; i < size; i++)
            {
                array[i] = in.nextInt();
            }
            return array;
        }

    }
}


	import java.util.*;
import java.io.*;
	public class Codeforces {
		public static void main(String[]YAHIA_MOSTAF) throws IOException ,InterruptedException {
			Scanner sc =new Scanner(System.in);
			PrintWriter pw =new PrintWriter(System.out);
			int n =sc.nextInt(),m=sc.nextInt();
			Integer[]arr =new Integer[n];
			for (int i=0;i<n;++i)
				arr[i]=sc.nextInt();
			Arrays.sort(arr);
			int [] sum =new int [n];
			int [] carry=new int[m];
			//System.out.println(Arrays.toString(arr));
			for (int i=0;i<n;++i) {
				carry[i%m]+=arr[i];
				sum[i]=carry[i%m];
				//System.out.println(Arrays.toString(carry)+" "+Arrays.toString(sum));
			}
			for (int i=1;i<n;++i)
				sum[i]+=sum[i-1];
			for (int i : sum)
				pw.print(i+" ");
			pw.close();
		}
		static class Scanner {
			StringTokenizer stringTokenizer;
			BufferedReader bfBufferedReader;
			public Scanner(InputStream system) {
				bfBufferedReader=new BufferedReader(new InputStreamReader( system));
			}
			 public Scanner(String file) throws Exception {
		            bfBufferedReader = new BufferedReader(new FileReader( file));
		        }
		        public String next() throws IOException {
		            while (stringTokenizer == null || !stringTokenizer.hasMoreTokens())
		                stringTokenizer = new StringTokenizer( bfBufferedReader.readLine());
		            return stringTokenizer.nextToken();
		        }

		        public String nextLine() throws IOException {
		            return bfBufferedReader.readLine();
		        }

		        public int nextInt() throws IOException {
		            return Integer.parseInt(next());
		        }

		        public double nextDouble() throws IOException {
		            return Double.parseDouble(next());
		        }

		        public char nextChar() throws IOException {
		            return next().charAt(0);
		        }

		        public Long nextLong() throws IOException {
		            return Long.parseLong(next());
		        }

		        public boolean ready() throws IOException {
		            return bfBufferedReader.ready();
		        }

		        public void waitForInput() throws InterruptedException {
		            Thread.sleep(3000);
		        }
		    }
	}


	import java.util.*;
import java.io.*;
	public class Codeforces {
		public static void main(String[]YAHIA_MOSTAF) throws IOException ,InterruptedException {
			Scanner sc =new Scanner(System.in);
			PrintWriter pw =new PrintWriter(System.out);
			int n =sc.nextInt(),m=sc.nextInt();
			Integer[]arr =new Integer[n];
			for (int i=0;i<n;++i)
				arr[i]=sc.nextInt();
			Arrays.sort(arr);
			long [] sum =new long [n];
			long [] carry=new long[m];
			//System.out.println(Arrays.toString(arr));
			for (int i=0;i<n;++i) {
				carry[i%m]+=arr[i];
				sum[i]=carry[i%m];
				//System.out.println(Arrays.toString(carry)+" "+Arrays.toString(sum));
			}
			for (int i=1;i<n;++i)
				sum[i]+=sum[i-1];
			for (long i : sum)
				pw.print(i+" ");
			pw.close();
		}
		static class Scanner {
			StringTokenizer stringTokenizer;
			BufferedReader bfBufferedReader;
			public Scanner(InputStream system) {
				bfBufferedReader=new BufferedReader(new InputStreamReader( system));
			}
			 public Scanner(String file) throws Exception {
		            bfBufferedReader = new BufferedReader(new FileReader( file));
		        }
		        public String next() throws IOException {
		            while (stringTokenizer == null || !stringTokenizer.hasMoreTokens())
		                stringTokenizer = new StringTokenizer( bfBufferedReader.readLine());
		            return stringTokenizer.nextToken();
		        }

		        public String nextLine() throws IOException {
		            return bfBufferedReader.readLine();
		        }

		        public int nextInt() throws IOException {
		            return Integer.parseInt(next());
		        }

		        public double nextDouble() throws IOException {
		            return Double.parseDouble(next());
		        }

		        public char nextChar() throws IOException {
		            return next().charAt(0);
		        }

		        public Long nextLong() throws IOException {
		            return Long.parseLong(next());
		        }

		        public boolean ready() throws IOException {
		            return bfBufferedReader.ready();
		        }

		        public void waitForInput() throws InterruptedException {
		            Thread.sleep(3000);
		        }
		    }
	}


import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    // decreasing array multipled by k+1 at every interval m for subarray array[n]
    int n = console.nextInt(), m = console.nextInt();
    // use a regular array because static memory allocation with wrapper class so we can reverse sort it
    Integer[] array = new Integer[n];
    for (int i = 0; i < n; i++) {
      array[i] = console.nextInt();
    }
    Arrays.sort(array, Collections.reverseOrder());
    // n is how many sweets she needs to eat per cycle
    // total is the minimum sugar penalty per cycle
    // d is the multipler
    // count is the interval checker
    // m is the interval
    while (n-- > 0) {
      int total = 0;
      int d = 1;
      int count = 0;
      for (int j = n; j < array.length; j++) {
        total += d * array[j];
        count++;
        if (count == m) {
          d++;
          count = 0;
        }
      }
      System.out.print(total);
    }
  }
}


import java.util.*;
import java.io.*;

//overall complexity is O(nlogn)
// space complexity is O(n)

public class Main {
  public static void main(String[] args) {
    FastScanner console = new FastScanner();
    PrintWriter out = new PrintWriter(System.out, true);
    int n = console.nextInt(), m = console.nextInt();
    long[] array = new long[n];
    // O(n);
    for (int i = 0; i < n; i++) {
      array[i] = console.nextLong();
    }
    // O(nlogn)
    Arrays.sort(array);
    // 2 3 4 4 6 6 7 8 19          original
    // 2 5 9 13 19 25 32 40 59     culmative sum
    // 2 5 11 18 30 43 62 83 121   answer
    // O(n) combined for loops
    out.printf("%s ", array[0]);
    for (int i = 1; i < m; i++) {
      array[i] += array[i-1];
      out.printf("%s ", array[i]);
    }
    int sub = 0;
    for (int i = m; i < n; i++) {
      if (sub == m) {
        sub = 0;
      }
      array[i] += array[i-1];
      array[sub] += array[i];
      out.printf("%s ", array[sub]);
      sub++;
    }
    out.flush();
    out.close();
  }

  public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }

        public FastScanner() {
            this(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.util.*;
import java.io.*;

// overall time complexity is O(nlogn)
// space complexity is O(n)

public class Main {

  public static void main(String[] args) {

    FastScanner console = new FastScanner();
    PrintWriter out = new PrintWriter(System.out, true);

    int n = console.nextInt(), m = console.nextInt();
    long[] arr = new long[n];

    // O(n);

    for (int i = 0; i < n; i++) {
      arr[i] = console.nextLong();
    }

    // O(nlogn)

    Arrays.sort(arr);

    // 2 3 4 4 6 6 7 8 19          original
    // 2 5 9 13 19 25 32 40 59     culmative sum
    // 2 5 11 18 30 43 62 83 121   answer

    StringBuilder sb = new StringBuilder();
    sb.append(arr[0] + " ");

    // O(n) combined for loops

    for (int i = 1; i < m; i++) {
      arr[i] += arr[i-1];
      sb.append(arr[i] + " ");
    }

    for (int i = m; i < n; i++) {
      arr[i] += arr[i-1];
      arr[i % m] += arr[i];
      sb.append(arr[i%m] + " ");
    }
    out.println(sb.toString().trim());

    out.flush();
    out.close();

  }

  public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }

        public FastScanner() {
            this(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.util.*;
import java.io.*;

// overall complexity is O(nlogn)
// space complexity is O(n)

public class Main {
  public static void main(String[] args) {
    FastScanner console = new FastScanner();
    PrintWriter out = new PrintWriter(System.out, true);
    int n = console.nextInt(), m = console.nextInt();
    Integer[] array = new Integer[n];
    // O(n);
    for (int i = 0; i < n; i++) {
      array[i] = console.nextInt();
    }
    // O(nlogn)
    Arrays.sort(array);
    // 2 3 4 4 6 6 7 8 19          original
    // 2 5 9 13 19 25 32 40 59     culmative sum
    // 2 5 11 18 30 43 62 83 121   answer
    // O(n) combined for loops
    out.printf("%s ", array[0]);
    for (int i = 1; i < m; i++) {
      array[i] += array[i-1];
      out.printf("%s ", array[i]);
    }
    for (int i = m; i < n; i++) {
      array[i] += array[i-1];
      array[i % m] += array[i];
      out.printf("%s ", array[i % m]);
    }
    out.flush();
    out.close();
  }

  public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }

        public FastScanner() {
            this(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    // decreasing array multipled by k+1 at every interval m for subarray array[n]
    int n = console.nextInt(), m = console.nextInt();
    // use a regular array because static memory allocation with wrapper class so we can reverse sort it
    Integer[] array = new Integer[n];
    for (int i = 0; i < n; i++) {
      array[i] = console.nextInt();
    }
    Arrays.sort(array, Collections.reverseOrder());
    // n is how many sweets she needs to eat per cycle
    // total is the minimum sugar penalty per cycle
    // d is the multipler
    // count is the interval checker
    // m is the interval
    while (n-- > 0) {
      int total = 0;
      int d = 1;
      int count = 0;
      for (int j = n; j < array.length; j++) {
        total += d * array[j];
        count++;
        if (count == m) {
          d++;
          count = 0;
        }
      }
      System.out.printf("%s ", total);
    }
  }
}


import java.util.*;
import java.io.*;

//overall complexity is O(nlogn)
// space complexity is O(n)

public class Main {
  public static void main(String[] args) {
    FastScanner console = new FastScanner();
    PrintWriter out = new PrintWriter(System.out, true);
    int n = console.nextInt(), m = console.nextInt();
    int[] array = new int[n];
    // O(n);
    for (int i = 0; i < n; i++) {
      array[i] = console.nextInt();
    }
    // O(nlogn)
    Arrays.sort(array);
    // 2 3 4 4 6 6 7 8 19          original
    // 2 5 9 13 19 25 32 40 59     culmative sum
    // 2 5 11 18 30 43 62 83 121   answer
    // O(n) combined for loops
    out.printf("%s ", array[0]);
    for (int i = 1; i < m; i++) {
      array[i] += array[i-1];
      out.printf("%s ", array[i]);
    }
    int sub = 0;
    for (int i = m; i < n; i++) {
      if (sub == m) {
        sub = 0;
      }
      array[i] += array[i-1];
      array[sub] += array[i];
      out.printf("%s ", array[sub]);
      sub++;
    }
    out.flush();
    out.close();
  }

  public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }

        public FastScanner() {
            this(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner console = new Scanner(System.in);
    int n = console.nextInt(), m = console.nextInt();
    Integer[] array = new Integer[n];
    // O(n);
    for (int i = 0; i < n; i++) {
      array[i] = console.nextInt();
    }
    // O(nlogn)
    Arrays.sort(array);
    // O(n);
     for (int i = 1; i < n; i++) {
      array[i] += array[i-1];
    }
    Integer[] answer = new Integer[n];
    // 2 3 4 4 6 6 7 8 19          original
    // 2 5 9 13 19 25 32 40 59     culmative sum
    // 2 5 11 18 30 43 62 83 121   answer
    // O(n) combined for loops
    for (int i = 0; i < m; i++) {
      answer[i] = array[i];
      System.out.printf("%s ", array[i]);
    }
    for (int i = m; i < n; i++) {
      answer[i] = array[i] + answer[i-m];
      System.out.printf("%s ", array[i] + answer[i-m]);
    }
  }
}


import java.util.*;
import java.io.*;

//overall complexity is O(nlogn)
// space complexity is O(n)

public class Main {
  public static void main(String[] args) {
    FastScanner console = new FastScanner();
    PrintWriter out = new PrintWriter(System.out, true);
    int n = console.nextInt(), m = console.nextInt();
    Long[] array = new Long[n];
    // O(n);
    for (int i = 0; i < n; i++) {
      array[i] = console.nextLong();
    }
    // O(nlogn)
    Arrays.sort(array);
    // 2 3 4 4 6 6 7 8 19          original
    // 2 5 9 13 19 25 32 40 59     culmative sum
    // 2 5 11 18 30 43 62 83 121   answer
    // O(n) combined for loops
    out.printf("%s ", array[0]);
    for (int i = 1; i < m; i++) {
      array[i] += array[i-1];
      out.printf("%s ", array[i]);
    }
    int sub = 0;
    for (int i = m; i < n; i++) {
      if (sub == m) {
        sub = 0;
      }
      array[i] += array[i-1];
      array[sub] += array[i];
      out.printf("%s ", array[sub]);
      sub++;
    }
    out.flush();
    out.close();
  }

  public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }

        public FastScanner() {
            this(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.util.*;
import java.io.*;

// overall complexity is O(nlogn)
// space complexity is O(n)

public class Main {
  public static void main(String[] args) {
    FastScanner console = new FastScanner();
    PrintWriter out = new PrintWriter(System.out, true);

    int n = console.nextInt(), m = console.nextInt();
    Integer[] arr = new Integer[n];

    // O(n);

    for (int i = 0; i < n; i++) {
      arr[i] = console.nextInt();
    }

    // O(nlogn)

    Arrays.sort(arr);

    // 2 3 4 4 6 6 7 8 19          original
    // 2 5 9 13 19 25 32 40 59     culmative sum
    // 2 5 11 18 30 43 62 83 121   answer

    StringBuilder sb = new StringBuilder();
    sb.append(arr[0] + " ");

    // O(n) combined for loops

    for (int i = 1; i < m; i++) {
      arr[i] += arr[i-1];
      sb.append(arr[i] + " ");
    }

    for (int i = m; i < n; i++) {
      arr[i] += arr[i-1];
      arr[i % m] += arr[i];
      sb.append(arr[i%m] + " ");
    }
    out.println(sb.toString().trim());

    out.flush();
    out.close();

  }

  public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }

        public FastScanner() {
            this(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


import java.util.*;
import java.io.*;

//overall complexity is O(nlogn)
// space complexity is O(n) f

public class Main {
  public static void main(String[] args) {
    FastScanner console = new FastScanner();
    PrintWriter out = new PrintWriter(System.out, true);
    int n = console.nextInt(), m = console.nextInt();
    Integer[] array = new Integer[n];
    // O(n);
    for (int i = 0; i < n; i++) {
      array[i] = console.nextInt();
    }
    // O(nlogn)
    Arrays.sort(array);
    // 2 3 4 4 6 6 7 8 19          original
    // 2 5 9 13 19 25 32 40 59     culmative sum
    // 2 5 11 18 30 43 62 83 121   answer
    // O(n) combined for loops
    out.printf("%s ", array[0]);
    for (int i = 1; i < m; i++) {
      array[i] += array[i-1];
      out.printf("%s ", array[i]);
    }
    int sub = 0;
    for (int i = m; i < n; i++) {
      if (sub == m) {
        sub = 0;
      }
      array[i] += array[i-1];
      array[sub] += array[i];
      out.printf("%s ", array[sub]);
      sub++;
    }
    out.flush();
    out.close();
  }

  public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }

        public FastScanner() {
            this(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
//int max=int_min;
int main()
{
    int q,bu,be,h,c,cc,profit;
    cin>>q;
    while(q>0)
    {
    	int count=0,count1=0;
    cin>>bu>>be>>cc>>h>>c;
    if( bu>=2)
    {
    	if(be<cc || (be==cc && h>c))
    	{
        while(be>0 && bu>=2)
        {
        count++;
        be--;
        bu=bu-2;
        }
        while(cc>0 && bu>=2)
        {
            count1++;
            cc--;
            bu=bu-2;
        }
        profit=(count*h)+(count1*c);
        cout<<profit<<endl;
    	}
    	else
    	{
    		while(cc>0 && bu>=2)
        {
        count++;
        cc--;
        bu=bu-2;
        }
        while(be>0 && bu>=2)
        {
            count1++;
            be--;
            bu=bu-2;
        }
        profit=(count*c)+(count1*h);
        cout<<profit<<endl;
		}
    }
    else
    cout<<0<<endl;
    q--;
    }

}


#include<bits/stdc++.h>
using namespace std;
int max=int_min;
int main()
{
    int q,bu,be,cc,count=0;
    cin>>q;
    while(q--)
    {
    cin>>bu>>be>>cc>>h>>c;
    if( bu>=2*be)
    {
        while(be>0)
        {
        count=count++;
        be--;
        }
        while(cc>0)
        {
            count1=count1--;
            cc--;
        }
        int profit=count*h+count1*c;
        return profit;
    }

    }
}


#include<bits/stdc++.h>
#include<algorithm>
using namespace std;

int main()
{
    int q,bu,be,h,c,cc,profit,k,l,m;
    cin>>q;
    while(q>0)
    {
    	int count=0,count1=0,check=1,i=2;
    cin>>bu>>be>>cc>>h>>c;
    		int k=be;
			int l=cc;
			int m=bu;
			int temp=h;
			int temp1=c;
    if( bu>=2)
    {
    	while(i>0)
    	{
        		while(be>0 && bu>=2)
        		{
        			count++;
        			be--;
        			bu=bu-2;
        		}
        		while(cc>0 && bu>=2)
        		{
            		count1++;
            		cc--;
            		bu=bu-2;
        		}
        		profit=(count*h)+(count1*c);
        		check=std::max(profit,check);
				be=l;
				cc=k;
				bu=m;
				h=temp1;
				c=temp;
				i--;
				count=0;
				count1=0;
		}
		cout<<check<<endl;
    }
    else
    cout<<0<<endl;
    q--;
    }
}


#include<bits/stdc++.h>
using namespace std;
//int max=int_min;
int main()
{
    int q,bu,be,h,c,cc,profit;
    cin>>q;
    while(q>0)
    {
    	int count=0,count1=0;
    cin>>bu>>be>>cc>>h>>c;
    if( bu>=2)
    {
    	if(be<cc || (be==cc && h>c))
    	{
        while(be>0 && bu>=2)
        {
        count++;
        be--;
        bu=bu-2;
        }
        while(cc>0 && bu>=2)
        {
            count1++;
            cc--;
            bu=bu-2;
        }
        profit=(count*h)+(count1*c);
        cout<<profit<<endl;
    	}
    	else
    	{
    		while(cc>0 && bu>=2)
        {
        count++;
        cc--;
        bu=bu-2;
        }
        while(be>0 && bu>=2)
        {
            count1++;
            be--;
            bu=bu-2;
        }
        profit=(count*c)+(count1*h);
        cout<<profit<<endl;
		}
    }
    else
    cout<<0;
    q--;
    }

}


#include<bits/stdc++.h>
using namespace std;
//int max=int_min;
int main()
{
    int q,bu,be,h,c,cc,profit;
    cin>>q;
    while(q>0)
    {
    	int count=0,count1=0;
    cin>>bu>>be>>cc>>h>>c;
    if( bu>=2)
    {
    	if(h<c && be<cc)
    	{
        while(be>0)
        {
        count++;
        be--;
        bu=bu-2;
        }
        while(cc>0 && bu>=2)
        {
            count1++;
            cc--;
            bu=bu-2;
        }
        profit=(count*h)+(count1*c);
        cout<<profit<<endl;
    	}
    	else
    	{
    		while(cc>0 && bu>=2)
        {
        count++;
        cc--;
        bu=bu-2;
        }
        while(be>0 && bu>=2)
        {
            count1++;
            be--;
            bu=bu-2;
        }
        profit=(count*c)+(count1*h);
        cout<<profit<<endl;
		}
    }
    else
    cout<<0;
    q--;
    }

}


#include<bits/stdc++.h>
using namespace std;
//int max=int_min;
int main()
{
    int q,bu,be,h,c,cc,profit;
    cin>>q;
    while(q>0)
    {
    	int count=0,count1=0;
    cin>>bu>>be>>cc>>h>>c;
    if( bu>=2)
    {
    	if(be<cc)
    	{
        while(be>0 && bu>=2)
        {
        count++;
        be--;
        bu=bu-2;
        }
        while(cc>0 && bu>=2)
        {
            count1++;
            cc--;
            bu=bu-2;
        }
        profit=(count*h)+(count1*c);
        cout<<profit<<endl;
    	}
    	else
    	{
    		while(cc>0 && bu>=2)
        {
        count++;
        cc--;
        bu=bu-2;
        }
        while(be>0 && bu>=2)
        {
            count1++;
            be--;
            bu=bu-2;
        }
        profit=(count*c)+(count1*h);
        cout<<profit<<endl;
		}
    }
    else
    cout<<0;
    q--;
    }

}


#include<bits/stdc++.h>
using namespace std;
//int max=int_min;
int main()
{
    int q,bu,be,h,c,cc,profit;
    cin>>q;
    while(q>0)
    {
    	int count=0,count1=0;
    cin>>bu>>be>>cc>>h>>c;
    if( bu>=2)
    {
    	if(be<cc)
    	{
        while(be>0)
        {
        count++;
        be--;
        bu=bu-2;
        }
        while(cc>0 && bu>=2)
        {
            count1++;
            cc--;
            bu=bu-2;
        }
        profit=(count*h)+(count1*c);
        cout<<profit<<endl;
    	}
    	else
    	{
    		while(cc>0 && bu>=2)
        {
        count++;
        cc--;
        bu=bu-2;
        }
        while(be>0 && bu>=2)
        {
            count1++;
            be--;
            bu=bu-2;
        }
        profit=(count*c)+(count1*h);
        cout<<profit<<endl;
		}
    }
    else
    cout<<0;
    q--;
    }

}


#include<bits/stdc++.h>
#include<algorithm>
using namespace std;

int main()
{
    int q,bu,be,h,c,cc,profit,k,l,m;
    cin>>q;
    while(q>0)
    {
    	int count=0,count1=0,check=1,i=2;
    cin>>bu>>be>>cc>>h>>c;
    		int k=be;
			int l=cc;
			int m=bu;
			int temp=h;
			int temp1=c;
    if( bu>=2)
    {
    	while(2)
    	{
        		while(be>0 && bu>=2)
        		{
        			count++;
        			be--;
        			bu=bu-2;
        		}
        		while(cc>0 && bu>=2)
        		{
            		count1++;
            		cc--;
            		bu=bu-2;
        		}
        		profit=(count*h)+(count1*c);
        		check=std::max(profit,check);
				be=l;
				cc=k;
				bu=m;
				h=temp1;
				c=temp;
				count=0;
				count1=0;
		}
		cout<<check<<endl;
    }
    else
    cout<<0<<endl;
    q--;
    }
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int b,p,f,h,c,a,d,i,t,sum,e,g,j,k;
    cin>>t;
    for(i=1; i<=t; i++)
    {
        cin>>b>>p>>f>>h>>c;
        sum=0;
        if(b>=2)
        {
            if(h<=c)
            {
                d=b/2;
                if(d>=f)
                {

                    e=f*c;
                    a=d-f;
                }
                if(a>=1)
                {
                    j=min(a,p);
                    g=j*h;
                }

            }
            if(h>c)
            {
                d=b/2;
                if(d>=p)
                {
                    e=p*h;
                    a=d-p;
                }
                if(a>=1)
                {
                    j=min(a,f);
                    g=j*c;
                }

            }
        sum+=e+g;
        }

        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int b,p,f,h,c,a,d,i,t,sum,e,g,j,k;
    cin>>t;
    for(i=1; i<=t; i++)
    {
        cin>>b>>p>>f>>h>>c;
        sum=0;
        if(b>p || b>f)
        {
            if(h<=c)
            {
                d=b/2;
                if(d>=f)
                {
                    k=min(d,f);
                    e=k*c;
                    a=d-f;
                }
                if(a>=1)
                {
                    j=min(a,p);
                    g=j*h;
                }

            }
            if(h>c)
            {
                d=b/2;
                if(d>=p)
                {
                    k=min(d,p);
                    e=p*h;
                    a=d-p;
                }
                if(a>=1)
                {
                    j=min(a,f);
                    g=j*c;
                }

            }
        sum+=e+g;
        }

        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int b,p,f,h,c,a,d,i,t,sum,e,g,j,k;
    cin>>t;
    for(i=1; i<=t; i++)
    {
        cin>>b>>p>>f>>h>>c;
        sum=0;
        d=b/2;
        if(b>p || b>f){
            if(h<c)
            {
                if(d>=f)
                {
                    sum+=f*c;
                    a=d-f;
                }
                if(a>=1)
                {
                    sum+=min(a,p)*h;

                }
                else
                    sum =a*c;

            }
            else
            {
                if(d>=p)
                {

                    sum+=p*h;
                    a=d-p;
                }
                if(a>=1)
                {
                    j=min(a,f);
                    sum+=j*c;
                }
                else
                    sum+=a*p;

            }
        }

        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
   long long  int b,p,f,h,c,a,d,i,t,sum,e,g,j,k;
    cin>>t;
    for(i=1; i<=t; i++)
    {
        cin>>b>>p>>f>>h>>c;
        sum=0;
        d=b/2;
        if(b>p || b>f){
            if(h<c)
            {
                if(d>=f)
                {
                    sum+=f*c;
                    a=d-f;
                }
                if(a>=1)
                {
                    sum+=min(a,p)*h;

                }
                else
                    sum =a*c;

            }
            else
            {
                if(d>=p)
                {

                    sum+=p*h;
                    a=d-p;
                }
                if(a>=1)
                {
                    j=min(a,f);
                    sum+=j*c;
                }
                else
                    sum+=a*p;

            }
        }

        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int b,p,f,h,c,a,d,i,t,sum,e,g,j,k;
    cin>>t;
    for(i=1; i<=t; i++)
    {
        cin>>b>>p>>f>>h>>c;
        sum=0;
        d=b/2;
        if(b>p || b>f)
        {
            if(h<c)
            {
                if(d>=f)
                {
                    sum+=f*c;
                    a=d-f;
                }
                if(a>=1)
                {
                    sum+=min(a,p)*h;

                }
                else
                    sum+=a*c;

            }
            else
            {
                if(d>=p)
                {

                    sum+=p*h;
                    a=d-p;
                }
                if(a>=1)
                {
                    j=min(a,f);
                    sum+=j*c;
                }
                else
                    sum+=a*b;

            }

        }

        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int b,p,f,h,c,a,d,i,t,sum,e,g,j,k;
    cin>>t;
    for(i=1; i<=t; i++)
    {
        cin>>b>>p>>f>>h>>c;
        sum=0;
        if(b>p || b>f)
        d=b/2;
        {
            if(h<c)
            {
                if(d>=f)
                {
                    sum+=f*c;
                    a=d-f;
                }
                if(a>=1)
                {
                    sum+=min(a,p)*h;

                }
                else
                    sum=d*c;

            }
            else
            {
                if(d>=p)
                {
                    k=min(d,p);
                    sum+=k*h;
                    a=d-p;
                }
                if(a>=1)
                {
                    j=min(a,f);
                    sum+=j*c;
                }
                else
                    sum=d*b;

            }

        }

        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int b,p,f,h,c,a,d,i,t,sum,e,g,j,k;
    cin>>t;
    for(i=1; i<=t; i++)
    {
        cin>>b>>p>>f>>h>>c;
        sum=0;
        if(b>=2)
        {
            if(h<=c)
            {
                d=b/2;
                if(d>=f)
                {
                    k=min(d,f);
                    e=k*c;
                    a=d-f;
                }
                if(a>=1)
                {
                    j=min(a,p);
                    g=j*h;
                }

            }
            if(h>c)
            {
                d=b/2;
                if(d>=p)
                {
                    k=min(d,p);
                    e=p*h;
                    a=d-p;
                }
                if(a>=1)
                {
                    j=min(a,f);
                    g=j*c;
                }

            }
        sum+=e+g;
        }

        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int b,p,f,h,c,a,d,i,t,sum,e,g,j,k;
    cin>>t;
    for(i=1; i<=t; i++)
    {
        cin>>b>>p>>f>>h>>c;
        sum=0;
        d=b/2;
        if(b>p || b>f)
        {
            if(h<c)
            {
                if(d>=f)
                {
                    sum+=f*c;
                    a=d-f;
                }
                if(a>=1)
                {
                    sum+=min(a,p)*h;

                }
                else
                    sum=a*c;

            }
            else
            {
                if(d>=p)
                {
                    k=min(d,p);
                    sum+=k*h;
                    a=d-p;
                }
                if(a>=1)
                {
                    j=min(a,f);
                    sum+=j*c;
                }
                else
                    sum=a*b;

            }

        }

        cout<<sum<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
   long long int t,ans;
    cin>>t;
    while(t--)
    {
        int n,p,f,a,b;
        cin>>n>>p>>f>>a>>b;
         ans = 0;
        n/=2;
        if(a>b)
        {
            if(p<=n)
            {
            n-=p;
            ans += a*p;
            if(n>=1)
            {
                ans += min(n,f)*b;
            }
            }
            else
                ans = n * a;
        }
        else
        {
            if(f<=n)
            {
            n-=f;
            ans += f*b;
            if(n>=1)
            {
                ans += min(n,p)*a;
            }
            }
            else
                ans = n * b;
        }
    cout<<ans<<endl;
    }
    return 0;
    }


#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int inp(){
	int Ans;char C;bool O=false;
	while((C=getchar())<'0'||C>'9')if(C=='-')O=true;Ans=C-48;
	while((C=getchar())>='0'&&C<='9')Ans=(Ans<<3)+(Ans<<1)+C-48;
	return O==true?-Ans:Ans;
}

int T;
int a,b,c;
int ans,x,y;

int main(){
	T=inp();
	while(T--){
		ans=0;
		a=inp(),b=inp(),c=inp();
		x=inp(),y=inp();
		int val,res;
		for(int i=1;i<=b;i++){
			for(int j=1;j<=c;j++){
				if(i*2+j*2>a)break;
				ans=max(i*x+j*y,ans);
			}
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int inp(){
	int Ans;char C;bool O=false;
	while((C=getchar())<'0'||C>'9')if(C=='-')O=true;Ans=C-48;
	while((C=getchar())>='0'&&C<='9')Ans=(Ans<<3)+(Ans<<1)+C-48;
	return O==true?-Ans:Ans;
}

int T;
int a,b,c;
int ans,x,y;

int main(){
	T=inp();
	while(T--){
		ans=0;
		a=inp(),b=inp(),c=inp();
		x=inp(),y=inp();
		int val,res;
		for(int i=0;i<=b;i++){
			for(int j=0;j<=c;j++){
				if(i*2+j*2>a)break;
				ans=max(i*x+j*y,ans);
			}
		}
		cout<<ans<<endl;
	}
}


#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int inp(){
	int Ans;char C;bool O=false;
	while((C=getchar())<'0'||C>'9')if(C=='-')O=true;Ans=C-48;
	while((C=getchar())>='0'&&C<='9')Ans=(Ans<<3)+(Ans<<1)+C-48;
	return O==true?-Ans:Ans;
}

int T;
int a,b,c;
int ans,x,y;

int main(){
	T=inp();
	while(T--){
		ans=0;
		a=inp(),b=inp(),c=inp();
		x=inp(),y=inp();
		int j,val,res;
		for(int i=1;i<=b;i++){
			if(i*2>a)break;
			val=i*x;
			res=a-(i*2);
			j=min(c,res/2);
			val+=j*y;
			ans=max(ans,val);
		}
		for(int i=1;i<=c;i++){
			if(i*2>a)break;
			val=i*y;
			res=a-(i*2);
			j=min(b,res/2);
			val+=j*x;
			ans=max(ans,val);
		}
		cout<<ans<<endl;
	}
}


#include<bits/stdc++.h>

#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back

/** Dateloper */

using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int b,p,f;
        int h,c;
        cin>>b>>p>>f>>h>>c;
        if(b>=p*2+f*2)
            cout<<p*5+f*c<<endl;
        else
        {
            if(h>=c)
                cout<<min(b/2,p)*h+min((b-min(b/2,p)*2)/2,f)*c<<endl;
            else
                cout<<min(b/2,f)*c+min((b-min(b/2,f)*2)/2,p)*h<<endl;

        }
    }
    return 0;
}


#include<bits/stdc++.h>

#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back

/** Dateloper */

using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int b,p,f;
        int h,c;
        cin>>b>>p>>f>>h>>c;
        if(b>=p*2+f*2)
            cout<<p*5+f*c<<endl;
        else
        {
            if(h>=c)
            {
                int tmp=min(b/2,p)*2;
                cout<<tmp*h+min((b-tmp)/2,f)*c;
            }
            else
            {
                int tmp=min(b/2,f)*2;
                cout<<tmp*c+min((b-tmp)/2,p)*h;

            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>

#define ll long long
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back

/** Dateloper */

using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int b,p,f;
        int h,c;
        cin>>b>>p>>f>>h>>c;
        if(b>=p*2+f*2)
            cout<<p*5+f*c<<endl;
        else
        {
            if(c>=h)
            {
                cout<<min(b/2,f)*c+min((b-min(b/2,f)*2)/2,p)*h<<endl;
            }
            else
                cout<<min(b/2,p)*h+min((b-min(b/2,p)*2)/2,f)*c<<endl;
        }
    }
    return 0;
}