제출 #997048

#제출 시각아이디문제언어결과실행 시간메모리
997048MilosMilutinovicGift (IZhO18_nicegift)C++14
26 / 100
435 ms110796 KiB
#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long s = accumulate(a.begin(), a.end(), 0LL);
  if (s % k != 0 || a[0] > s / k) {
    cout << -1 << '\n';
    return 0;
  }
  set<pair<long long, int>> st;
  for (int i = 0; i < n; i++) {
    st.emplace(a[i], i);
  }
  vector<vector<long long>> res;
  while (s > 0) {
    vector<int> ids;
    for (int foo = 0; foo < k; foo++) {
      auto it = prev(st.end());
      ids.push_back(it->second);
      st.erase(it);
    }
    long long v = a[ids.back()];
    if (!st.empty()) {
      v = min(v, s / k - prev(st.end())->first);
    }
    res.push_back({v});
    for (int i : ids) {
      res.back().push_back(i + 1);
    }
    for (int i : ids) {
      s -= v;
      a[i] -= v;
      if (a[i] > 0) {
        st.emplace(a[i], i);
      }
    }
  }
  cout << (int) res.size() << '\n';
  for (int i = 0; i < (int) res.size(); i++) {
    for (int j = 0; j < (int) res[i].size(); j++) {
      cout << res[i][j] << " ";
    }
    cout << '\n';
  }
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...