This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
/// toto -> 1
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define io \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define all(a) a.begin(),a.end()
#define rep(k,i,n) for(int k=i;k<n;k++)
#define repp(k,i,n) for(int k=n-1;k>=i;k--)
#define rech(k) for(char k='a';k<='z';k++)
#define SZ(a) (int)a.size()
#define MX(a) *max_element(all(a))
#define MN(a) *min_element(all(a))
#define SM(a) accumulate(all(a),0LL)
#define vi vector<int>
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define vvi vector<vector<int>>
/// toto -> 2
const int modd = 1e9 + 7 , mod = 998244353;
typedef long long ll;
void F1(bool res) {
if(res) {
cout<<"Yes\n" ;
}
else {
cout<<"No\n" ;
}
}
struct mint{
ll val;
mint(int64_t v = 0) {
v %= mod;
if (v < 0) v += mod;
val = v;
}
mint& operator+=(const mint& other) {
val += other.val;
if (val >= mod) val -= mod;
return *this;
}
mint& operator-=(const mint& other) {
val += mod - other.val;
if (val >= mod) val -= mod;
return *this;
}
mint& operator*=(const mint& other) {
val = (int64_t)val * other.val % mod;
return *this;
}
mint& operator/=(const mint& other) {
return *this *= other.inv();
}
friend mint operator+(mint a, const mint& b) { return a += b; }
friend mint operator-(mint a, const mint& b) { return a -= b; }
friend mint operator*(mint a, const mint& b) { return a *= b; }
friend mint operator/(mint a, const mint& b) { return a /= b; }
mint pow(int64_t exp) const {
mint a = *this, res = 1;
while (exp > 0) {
if (exp & 1)
res *= a;
a *= a;
exp >>= 1;
}
return res;
}
mint inv() const {
assert(val != 0);
return pow(mod - 2);
}
friend ostream& operator<<(ostream& os, const mint& m) {
return os << m.val;
}
};
ll bp(ll n,ll m){
if(m == 0){
return 1;
}
if(m == 1){
return n%mod;
}
if(m%2==0){
return bp(n*n%mod,m/2)%mod;
}
return n*bp(n,m-1)%mod;
}
/// toto -> 3
const int N = 2e5 + 545, M = 2e4 + 434, inf = 1e9 + 99;
const ll inff = 1e12;
#define int long long
void solve() {
int n,k;
cin >> n >> k;
set<pair<int , int>> q;
vector<vector<int>> res;
int sum = 0;
rep(i,0,n) {
int x;
cin >> x;
q.insert({x , i});
sum += x;
}
if(sum%k != 0) {
cout << -1 << endl;
return;
}
for(int i=0;i<n - 1;i++) {
if(sum == 0) {
break;
}
if(q.size() == 0) {
break;
}
int pos = (*q.begin()).second;
int val = (*q.begin()).first;
q.erase(q.begin());
sum -= val;
while(val--) {
if(sum == 0) {
break;
}
if((int)q.size() < k - 1) {
cout << "-1 "<< endl;
return;
}
vector<int> ind , tmp(k);
vector<int> ind2(k);
ind.push_back(1);
for(int j=k - 1;j>=1;j--) {
tmp[j] = (*q.rbegin()).first;
ind.push_back((*q.rbegin()).second + 1);
ind2[j] = (*q.rbegin()).second + 1;
q.erase(--q.end());
}
ind.push_back(pos + 1);
if(tmp[1] == 0) {
cout << "-1 "<< endl;
return;
}
res.push_back(ind);
for(int j=1;j<k;j++) {
if(tmp[j] == 1) {
continue;
}
tmp[j]--;
sum--;
q.insert({tmp[j] , ind2[j] - 1});
}
}
}
if(q.size() > 0) {
cout << "-1 "<< endl;
return;
}
cout << res.size() << endl;
for(auto i:res) {
for(auto j:i) {
cout << j << " ";
}
cout << endl;
}
cout << endl;
}
int32_t main() {
io;
ll t=1;
/// cin>>t;
rep(_,1,t+1) {
cerr << "Start of test case " << _ << "\n";
///cout<<"Case #"<<_<<": ";
solve();
cerr << endl;
cerr << endl;
}
return 0;
}
# | 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... |