Language: C++

#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for(int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL


s64 N, M;
vs64 A, S, sum;

s64 cost(s64 x)
{
    s64 ret = 0;
    rep (i, N)
    {
        ret += S[((x - A[i] >= 0) ? x - A[i] : 0)];
    }
    return ret;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> N >> M;
    A.resize(N);
    S.resize(1e5 + 1, 0);
    sum.resize(N + 1);
    sum[0] = 0;
    rep (i, N)
    {
        cin >> A[i];
        ++S[A[i]];
    }

    sort(A.begin(), A.end(), greater<s64>());
    rep (i, N) sum[i + 1] = sum[i] + A[i];
    for (int i = A[0]; i > 0; --i)
    {
        S[i - 1] += S[i];
    }

    s64 l = 2 * A[N - 1];
    s64 r = 2 * A[0] + 1;
    s64 mid;
    while (l + 1 < r)
    {
        mid = (l + r) / 2;
        s64 c = cost(mid);
        if (c >= M)
        {
            l = mid;
        }
        else
        {
            r = mid;
        }
    }

    s64 ans = ((M - cost(r)) >= ? M - cost(r) : 0) * l;
    M = (cost(r) > M ? M : cost(r));
    rep (i, N)
    {
        if (M <= 0) break;
        s64 idx = S[((r - A[i] >= 0) ? r - A[i] : 0)];
        if (idx > M) idx = M;
        ans += idx * A[i] + sum[idx];
        M -= idx;
    }

    cout << ans << "\n";
    return 0;
}



Compiler output:
a.cc:18:9: error: 'uint64_t' does not name a type
   18 | typedef uint64_t u64;
      |         ^~~~~~~~
a.cc:15:1: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
   14 | #include <cstring>
  +++ |+#include <cstdint>
   15 | 
a.cc:20:9: error: 'uint32_t' does not name a type
   20 | typedef uint32_t u32;
      |         ^~~~~~~~
a.cc:20:9: note: 'uint32_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
a.cc:23:16: error: 'u32' was not declared in this scope; did you mean 's32'?
   23 | typedef vector<u32> vu32;
      |                ^~~
      |                s32
a.cc:23:19: error: template argument 1 is invalid
   23 | typedef vector<u32> vu32;
      |                   ^
a.cc:23:19: error: template argument 2 is invalid
a.cc:25:16: error: 'u64' was not declared in this scope; did you mean 's64'?
   25 | typedef vector<u64> vu64;
      |                ^~~
      |                s64
a.cc:25:19: error: template argument 1 is invalid
   25 | typedef vector<u64> vu64;
      |                   ^
a.cc:25:19: error: template argument 2 is invalid
a.cc: In function 'int main()':
a.cc:91:33: error: expected primary-expression before '?' token
   91 |     s64 ans = ((M - cost(r)) >= ? M - cost(r) : 0) * l;
      |                                 ^