fork download
  1. #include <bits/stdc++.h>
  2. #define endl '\n'
  3.  
  4. //#pragma GCC optimize ("O3")
  5. //#pragma GCC target ("sse4")
  6.  
  7. using namespace std;
  8. template<class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
  9. template<class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
  10. const int MAXN = (1 << 20);
  11.  
  12. int n, m;
  13. int a[MAXN];
  14.  
  15. void read()
  16. {
  17. cin >> n >> m;
  18. for(int i = 0; i < m; i++)
  19. cin >> a[i];
  20. }
  21.  
  22. void solve()
  23. {
  24. sort(a, a + m);
  25.  
  26. priority_queue<pair<int64_t, int> > pq;
  27. for(int i = 0; i < m; i++)
  28. pq.push({-a[i], i});
  29.  
  30. int64_t sum = 0;
  31. for(int i = 0; i < n; i++)
  32. {
  33. auto it = pq.top();
  34. sum -= it.first;
  35. pq.pop();
  36.  
  37. for(int j = it.second; j < m; j++)
  38. pq.push({it.first - a[j], j});
  39. }
  40.  
  41. cout << sum << endl;
  42. }
  43.  
  44. int main()
  45. {
  46. ios_base::sync_with_stdio(false);
  47. cin.tie(NULL);
  48.  
  49. read();
  50. solve();
  51. return 0;
  52. }
  53.  
  54.  
Success #stdin #stdout 0s 19336KB
stdin
Standard input is empty
stdout
0