/**
* LES GREATEABLES BRO TEAM
**/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(x) (int)x.size()
const bool FLAG = false;
void setIO(const string &f = "");
string to_string(const string s) {
return '"' + s + '"';
}
string to_string(const char c) {
return char(39) + string(1, c) + char(39);
}
string to_string(const char* s) {
return to_string(string(s));
}
string to_string(bool f) {
return (f ? "true" : "false");
}
template<class A, class B>
string to_string(const pair<A, B> x) {
return "(" + to_string(x.first) + ", " + to_string(x.second) + ")";
}
template<class A, class B, class C>
string to_string(const tuple<A, B, C> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ")";
}
template<class A, class B, class C, class D>
string to_string(const tuple<A, B, C, D> x) {
return "(" + to_string(get<0>(x)) + ", " + to_string(get<1>(x)) + ", " + to_string(get<2>(x)) + ", " + to_string(get<3>(x)) + ")";
}
template<class T>
string to_string(const T v) {
string res = "{"; bool f = true;
for (auto x: v)
res += (f ? to_string(x) : ", " + to_string(x)), f = false;
return res + "}";
}
void debug_args() { cerr << "]\n"; }
template<class H, class... T>
void debug_args(H x, T... y) {
cerr << to_string(x);
if (sizeof... (y))
cerr << ", ";
debug_args(y...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: [", debug_args(__VA_ARGS__);
#else
#define debug(...) 47
#endif
#define int ll
const int N = 2e6 + 4;
ll n, k, a[N];
void solve() {
cin >> n >> k;
ll sum = 0, mx = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mx = max(mx, a[i]);
sum += a[i];
}
if (sum % k != 0 || mx > sum / k) {
cout << -1;
return;
}
sum /= k;
ll cur_sum = 0;
deque<pair<int, int>> s[k + k];
int id = 1;
for (int i = 1; i <= n; i++) {
if (cur_sum + a[i] <= sum) {
cur_sum += a[i];
s[id].push_back({a[i], i});
if (cur_sum == sum) {
id++;
cur_sum = 0;
}
} else if (cur_sum + a[i] > sum) {
int y = cur_sum + a[i] - sum, x = a[i] - y;
s[id].push_back({x, i});
id++;
s[id].push_back({y, i});
cur_sum = y;
}
}
assert(cur_sum == 0);
if (id != k + 1) {
cout << -1;
return;
}
for (int i = 1; i <= k; i++) {
ll check = 0;
for (int j = 0; j < sz(s[i]); j++) {
check += s[i][j];
}
if (check != sum) {
cout << -1;
return;
}
}
vector<vector<int>> ans;
while (true) {
int mx = 1000000000;
for (int i = 1; i <= k; i++) {
mx = min(mx, s[i].front().first);
}
vector<int> cur;
cur.push_back(mx);
for (int i = 1; i <= k; i++) {
cur.push_back(s[i].front().second);
s[i].front().first -= mx;
if (!s[i].front().first) {
s[i].pop_front();
}
}
ans.push_back(cur);
bool flag = 0;
for (int i = 1; i <= k; i++) {
while (sz(s[i]) && !s[i].front().first)
s[i].pop_front();
if (sz(s[i]) <= 0) {
flag = 1;
break;
}
}
if (flag)
break;
}
cout << sz(ans) << '\n';
for (auto x: ans) {
for (auto y: x)
cout << y << ' ';
cout << '\n';
}
}
signed main() {
setIO();
int tt = 1;
if (FLAG) {
cin >> tt;
}
while (tt--) {
solve();
}
return 0;
}
void setIO(const string &f) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen((f + ".in").c_str(), "r")) {
freopen((f + ".in").c_str(), "r", stdin);
freopen((f + ".out").c_str(), "w", stdout);
}
}
Compilation message
nicegift.cpp: In function 'void solve()':
nicegift.cpp:106:10: error: no match for 'operator+=' (operand types are 'll' {aka 'long long int'} and '__gnu_cxx::__alloc_traits<std::allocator<std::pair<long long int, long long int> >, std::pair<long long int, long long int> >::value_type' {aka 'std::pair<long long int, long long int>'})
106 | check += s[i][j];
nicegift.cpp: In function 'void setIO(const string&)':
nicegift.cpp:167:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
167 | freopen((f + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nicegift.cpp:168:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
168 | freopen((f + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~