/**
* 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 + 1];
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;
}
}
for (int i = 1; i <= k; i++) {
ll check = 0;
for (int j = 0; j < sz(s[i]); j++) {
check += s[i][j].first;
}
if (check != sum) {
assert(false);
}
}
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 setIO(const string&)':
nicegift.cpp:162:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
162 | freopen((f + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nicegift.cpp:163:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
163 | freopen((f + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
1 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
1 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
0 ms |
204 KB |
n=5 |
8 |
Correct |
1 ms |
204 KB |
n=8 |
9 |
Correct |
0 ms |
204 KB |
n=14 |
10 |
Correct |
0 ms |
204 KB |
n=11 |
11 |
Correct |
21 ms |
3824 KB |
n=50000 |
12 |
Correct |
21 ms |
3576 KB |
n=50000 |
13 |
Correct |
0 ms |
204 KB |
n=10 |
14 |
Correct |
2 ms |
332 KB |
n=685 |
15 |
Correct |
1 ms |
332 KB |
n=623 |
16 |
Correct |
1 ms |
332 KB |
n=973 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
1 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
0 ms |
204 KB |
n=5 |
8 |
Correct |
1 ms |
204 KB |
n=8 |
9 |
Correct |
0 ms |
204 KB |
n=14 |
10 |
Correct |
0 ms |
204 KB |
n=11 |
11 |
Correct |
21 ms |
3824 KB |
n=50000 |
12 |
Correct |
21 ms |
3576 KB |
n=50000 |
13 |
Correct |
0 ms |
204 KB |
n=10 |
14 |
Correct |
2 ms |
332 KB |
n=685 |
15 |
Correct |
1 ms |
332 KB |
n=623 |
16 |
Correct |
1 ms |
332 KB |
n=973 |
17 |
Correct |
2 ms |
332 KB |
n=989 |
18 |
Correct |
1 ms |
324 KB |
n=563 |
19 |
Correct |
2 ms |
432 KB |
n=592 |
20 |
Correct |
2 ms |
440 KB |
n=938 |
21 |
Correct |
2 ms |
460 KB |
n=747 |
22 |
Correct |
2 ms |
460 KB |
n=991 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1283 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
n=4 |
2 |
Correct |
0 ms |
204 KB |
n=3 |
3 |
Correct |
0 ms |
204 KB |
n=3 |
4 |
Correct |
1 ms |
204 KB |
n=4 |
5 |
Correct |
0 ms |
204 KB |
n=4 |
6 |
Correct |
0 ms |
204 KB |
n=2 |
7 |
Correct |
0 ms |
204 KB |
n=5 |
8 |
Correct |
1 ms |
204 KB |
n=8 |
9 |
Correct |
0 ms |
204 KB |
n=14 |
10 |
Correct |
0 ms |
204 KB |
n=11 |
11 |
Correct |
21 ms |
3824 KB |
n=50000 |
12 |
Correct |
21 ms |
3576 KB |
n=50000 |
13 |
Correct |
0 ms |
204 KB |
n=10 |
14 |
Correct |
2 ms |
332 KB |
n=685 |
15 |
Correct |
1 ms |
332 KB |
n=623 |
16 |
Correct |
1 ms |
332 KB |
n=973 |
17 |
Correct |
2 ms |
332 KB |
n=989 |
18 |
Correct |
1 ms |
324 KB |
n=563 |
19 |
Correct |
2 ms |
432 KB |
n=592 |
20 |
Correct |
2 ms |
440 KB |
n=938 |
21 |
Correct |
2 ms |
460 KB |
n=747 |
22 |
Correct |
2 ms |
460 KB |
n=991 |
23 |
Runtime error |
1283 ms |
524292 KB |
Execution killed with signal 9 |
24 |
Halted |
0 ms |
0 KB |
- |