#include <bits/stdc++.h>
#define respagold ios_base::sync_with_stdio(0), cin.tie(0);
#define int long long
#define ll long long
#define int2 __int128_t
#define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
#define FORR( i, x, n, d ) for( int i = x; i >= n; i -= d )
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define pb push_back
#define ins insert
#define lb lower_bound
#define ub upper_bound
#define pii pair <int, int>
#define mkp make_pair
using namespace std;
const int N = 1e6 + 123;
const int inf = 1e18;
const int mod = 1e9 + 7;
int a[N], b[N], c[N], n, m, q, k, z, w, x, y;
mt19937 rng( chrono::steady_clock::now().time_since_epoch().count());
int rand( int l, int r )
{
uniform_int_distribution <int> uid( l, r );
return uid( rng );
}
vector <vector <int>> ans;
vector <int> sv, v;
void solve()
{
cin >> n >> k;
multiset <pii> st;
FOR( i, 1, n, 1 ) cin >> a[i], x += a[i], st.ins({ a[i], i });
if( x % k || (*st.rbegin()).F > x / k )
{
cout << "-1\n";
return;
}
bool red = 0;
while( 1 )
{
v.clear();
while( sz(v) < k && sz(st) ) v.pb( (*st.rbegin()).S ), st.erase( st.find( *st.rbegin() ));
int cc = a[v.back()];
sv.clear();
int bob = -1000000;
if( sz(st) ) cc = min ( cc, x / k - (*st.rbegin()).S );
sv.pb(cc);
if( cc == 0 && a[v[0]] )
{
cout << -1;
return;
}
x -= cc * k;
for( auto i : v )
{
a[i] -= cc;
if( a[i] > 0 ) st.ins({ a[i], i });
sv.pb(i);
}
ans.pb(sv);
if( sz(st) == 0 ) break;
}
cout << sz(ans) << '\n';
for( auto i : ans )
{
for( auto j : i ) cout << j << ' ';
cout << '\n';
}
}
signed main()
{
// freopen("connect.in", "r", stdin);
// freopen("connect.out", "w", stdout);
respagold
int test = 1;
if( !test ) cin >> test;
while( test -- )
{
solve();
}
}
// solved by KluydQ
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |