結果

問題 No.615 集合に分けよう
ユーザー 0w1
提出日時 2017-12-26 20:26:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 680 bytes
コンパイル時間 2,426 ms
コンパイル使用メモリ 201,944 KB
最終ジャッジ日時 2025-01-05 06:31:59
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

long long greedy(int n, int k, const std::vector<long long> &a) {
  std::vector<long long> aa = a;
  std::sort(aa.begin(), aa.end());
  std::vector<long long> xa(n - 1);
  for (int i = 0; i + 1 < n; ++i) {
    xa[i] = aa[i + 1] - aa[i];
  }
  std::sort(xa.begin(), xa.end(), std::greater<long long>());
  long long res = aa.back() - aa.front();
  for (int i = 0; i + 1 < k; ++i) {
    res -= xa[i];
  }
  return res;
}

signed main() {
  std::ios::sync_with_stdio(false);
  int N, K;
  std::cin >> N >> K;
  std::vector<long long> A(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
  }
  std::cout << greedy(N, K, A) << std::endl;
  return 0;
}
0