Problem:
Score : 500 points

Problem Statement

Takahashi has come to a party as a special guest.
There are N ordinary guests at the party. The i-th ordinary guest has a power of A_i.

Takahashi has decided to perform M handshakes to increase the happiness of the party (let the current happiness be 0).
A handshake will be performed as follows:

Takahashi chooses one (ordinary) guest x for his left hand and another guest y for his right hand (x and y can be the same).

Then, he shakes the left hand of Guest x and the right hand of Guest y simultaneously to increase the happiness by A_x+A_y.

However, Takahashi should not perform the same handshake more than once. Formally, the following condition must hold:

Assume that, in the k-th handshake, Takahashi shakes the left hand of Guest x_k and the right hand of Guest y_k. Then, there is no pair p, q (1 \leq p < q \leq M) such that (x_p,y_p)=(x_q,y_q).

What is the maximum possible happiness after M handshakes?

Constraints

1 \leq N \leq 10^5

1 \leq M \leq N^2

1 \leq A_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the maximum possible happiness after M handshakes.

Sample Input 1

5 3
10 14 19 34 33

Sample Output 1

202

Let us say that Takahashi performs the following handshakes:

In the first handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 4.

In the second handshake, Takahashi shakes the left hand of Guest 4 and the right hand of Guest 5.

In the third handshake, Takahashi shakes the left hand of Guest 5 and the right hand of Guest 4.

Then, we will have the happiness of (34+34)+(34+33)+(33+34)=202.

We cannot achieve the happiness of 203 or greater, so the answer is 202.

Sample Input 2

9 14
1 3 5 110 24 21 34 5 3

Sample Output 2

1837

Sample Input 3

9 73
67597 52981 5828 66249 75177 64141 40773 79105 16076

Sample Output 3

8128170

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

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

// Source: https://www.creativ.xyz/fast-fourier-transform/
class CooleyTukeyFftConvolutionCalculator {
  private static class ComplexDouble {
    final double real, imag;
    
    ComplexDouble(double real, double imag) {
      this.real = real;
      this.imag = imag;
    }
    
    static ComplexDouble add(ComplexDouble a, ComplexDouble b) {
      return new ComplexDouble(a.real + b.real, a.imag + b.imag);
    }
    
    static ComplexDouble sub(ComplexDouble a, ComplexDouble b) {
      return new ComplexDouble(a.real - b.real, a.imag - b.imag);
    }
    
    static ComplexDouble mul(ComplexDouble a, double b) {
      return new ComplexDouble(a.real * b, a.imag * b);
    }
    
    static ComplexDouble mul(ComplexDouble a, ComplexDouble b) {
      double mulReal = a.real * b.real - a.imag * b.imag;
      double mulImag = a.real * b.imag + a.imag * b.real;
      return new ComplexDouble(mulReal, mulImag);
    }
    
    static ComplexDouble polar(double abs, double theta) {
      return new ComplexDouble(abs * Math.cos(theta), abs * Math.sin(theta));
    }
    
    public String toString() {
      return String.format("(%.3f, %.3f)", real, imag);
    }
  }
  
  private static ComplexDouble[] fft(ComplexDouble[] vs, boolean inverse) {
    int n = vs.length;
    int log2N = 0;
    for (int i = 0; (1 << i) < n; i++) {
      log2N++;
    }
    for (int i = 0; i < n; i++) {
      int j = 0;
      for (int k = 0; k < log2N; k++) {
        j |= (i >> k & 1) << (log2N - 1 - k);
      }
      if (i < j) {
        ComplexDouble swap = vs[i];
        vs[i] = vs[j];
        vs[j] = swap;
      }
    }
    for (int b = 1; b < n; b *= 2) {
      for (int j = 0; j < b; j++) {
        ComplexDouble w = ComplexDouble.polar(1.0, (2.0 * Math.PI) / (2.0 * b) * j * (inverse ? 1.0 : -1.0));
        for (int k = 0; k < n; k += b * 2) {
          ComplexDouble s = vs[j + k];
          ComplexDouble t = ComplexDouble.mul(vs[j + k + b], w);
          vs[j + k] = ComplexDouble.add(s, t);
          vs[j + k + b] = ComplexDouble.sub(s, t);
        }
      }
    }
    if (inverse) {
      for (int i = 0; i < n; i++) {
        vs[i] = ComplexDouble.mul(vs[i], 1.0 / n);
      }
    }
    return vs;
  }
  
  private static ComplexDouble[] expand(double[] vs, int size) {
    ComplexDouble[] cvs = new ComplexDouble[size];
    for (int i = 0; i < vs.length; i++) {
      cvs[i] = new ComplexDouble(vs[i], 0);
    }
    for (int i = vs.length; i < size; i++) {
      cvs[i] = new ComplexDouble(0, 0);
    }
    return cvs;
  }
  
  public static double[] convolve(double[] as, double[] bs) {
    int s = as.length + bs.length - 1;
    int t = 1;
    while (t < s) {
      t *= 2;
    }
    ComplexDouble[] expandedAs = expand(as, t);
    ComplexDouble[] expandedBs = expand(bs, t);
    ComplexDouble[] fftAs = fft(expandedAs, false);
    ComplexDouble[] fftBs = fft(expandedBs, false);
    ComplexDouble[] fftCs = new ComplexDouble[t];
    for (int i = 0; i < t; i++) {
      fftCs[i] = ComplexDouble.mul(fftAs[i], fftBs[i]);
    }
    
    ComplexDouble[] expandedCs = fft(fftCs, true);
    
    double[] cs = new double[s];
    for (int i = 0; i < s; i++) {
      cs[i] = expandedCs[i].real;
    }
    return cs;
  }
}

class Solver {
  private static final int AMAX = 100_000;
  final int n;
  final long m;
  final int[] as;
  
  Solver(int n, long m, int[] as) {
    this.n = n;
    this.m = m;
    this.as = as;
  }
  
  public long solve() {
    int aMax = 0;
    for (int a : as) {
      aMax = Math.max(aMax, a);
    }
    double[] vs = new double[aMax + 1];
    for (int a : as) {
      vs[a] += 1.0;
    }
    
    double[] convolvedVs = CooleyTukeyFftConvolutionCalculator.convolve(vs, vs);
    int[] countPairValues = new int[convolvedVs.length];
    for (int i = 0; i < convolvedVs.length; i++) {
      countPairValues[i] = (int)(convolvedVs[i] + 0.5);
    }
    
    long answer = 0;
    long restM = m;
    for (int i = countPairValues.length - 1; i >= 0 && restM > 0; i--) {
      if (countPairValues[i] < restM) {
        answer += (long) countPairValues[i] * (long) i;
        restM -= countPairValues[i];
      } else {
        answer += (long) restM * (long) i;
        restM = 0;
      }
    }
    return answer;
  }
}

public class Main {
  private static void execute(ContestReader reader, PrintWriter out) {
    int n = reader.nextInt();
    long m = reader.nextLong();
    int[] as = reader.nextInt(n);
    out.println(new Solver(n, m, as).solve());
  }
  
  public static void main(String[] args) {
    ContestReader reader = new ContestReader(System.in);
    PrintWriter out = new PrintWriter(System.out);
    execute(reader, out);
    out.flush();
  }
}

class ContestReader {
  private BufferedReader reader;
  private StringTokenizer tokenizer;
  
  ContestReader(InputStream in) {
    reader = new BufferedReader(new InputStreamReader(in));
  }
  
  public String next() {
    while (tokenizer == null || !tokenizer.hasMoreTokens()) {
      try {
        tokenizer = new java.util.StringTokenizer(reader.readLine());
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return tokenizer.nextToken();
  }
  
  public int nextInt() {
    return Integer.parseInt(next());
  }
  
  public long nextLong() {
    return Long.parseLong(next());
  }
  
  public double nextDouble() {
    return Double.parseDouble(next());
  }
  
  public String[] next(int n) {
    String[] array = new String[n];
    for (int i = 0; i < n; i++) {
      array[i] = next();
    }
    return array;
  }
  
  public int[] nextInt(int n) {
    int[] array = new int[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextInt();
    }
    return array;
  }
  
  public long[] nextLong(int n) {
    long[] array = new long[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextLong();
    }
    return array;
  }
  
  public double[] nextDouble(int n) {
    double[] array = new double[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextDouble();
    }
    return array;
  }
  
  public char[] nextCharArray() {
    return next().toCharArray();
  }
  
  public int[][] nextInt(int n, int m) {
    int[][] matrix = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextInt();
      }
    }
    return matrix;
  }
  
  public long[][] nextLong(int n, int m) {
    long[][] matrix = new long[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextLong();
      }
    }
    return matrix;
  }
  
  public double[][] nextDouble(int n, int m) {
    double[][] matrix = new double[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextDouble();
      }
    }
    return matrix;
  }
  
  public char[][] nextCharArray(int n) {
    char[][] matrix = new char[n][];
    for (int i = 0; i < n; i++) {
      matrix[i] = next().toCharArray();
    }
    return matrix;
  }
}