fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ivec = vector<int>;
  4. const int mod = 1e9+7;
  5.  
  6. struct products {
  7. int maxScore = 0;
  8. inline int sum(const ivec &a, int u, int v) {
  9. int s = 0;
  10. while (u < v)
  11. if (s += a[u++], s >= mod)
  12. s -= mod;
  13. return s; }
  14. inline void mac(const ivec &a, int i, int u, int v) {
  15. long long w = 1ll*i*sum(a,u,v);
  16. if (w %= mod, maxScore += w, maxScore >= mod)
  17. maxScore -= mod; }
  18. inline products(ivec &a, int m) {
  19. int n = a.size(), p = n/m, u = 0, v = m;
  20. sort(a.begin(),a.end());
  21. for (int i = 1; i < p; ++i)
  22. mac(a,i,u,v), u = v, v += m;
  23. mac(a,p,u,n); } };
  24.  
  25. inline int maxScore(ivec &a, int m) { return products(a,m).maxScore; }
  26.  
  27. int main() {
  28. int n, m; ivec a;
  29. ios_base::sync_with_stdio(0), cin.tie(0), cin >> n >> m, a.resize(n);
  30. for (auto &b: a)
  31. cin >> b;
  32. cout << maxScore(a,m); }
  33.  
Success #stdin #stdout 0s 4116KB
stdin
5 2
1 5 4 2 3
stdout
27