forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA.cpp
More file actions
84 lines (75 loc) · 1.85 KB
/
A.cpp
File metadata and controls
84 lines (75 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;
const long long oo = 1LL << 60;
int n, m, ans[33], at[33];
pair <int,int> tr[33][1111];
pair <long long,int> a[33];
long long f[33][1111];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
freopen("cookies.in", "r", stdin);
freopen("cookies.out", "w", stdout);
cin >> n >> m;
if (m % n == 0)
{
cout << 0 << endl;
for (int i = 1; i <= n; i++)
cout << m / n << ' ';
return 0;
}
for (int i = 1; i <= n; i++)
{
cin >> a[i].first;
a[i].second = i;
}
sort(a + 1, a + n + 1);
if (m > n * (n + 1))
{
cout << a[1].first * (n - 1LL) << endl;
m -= n;
ans[a[1].second] = m % (n - 1) + 1;
for (int i = 2; i <= n; i++)
ans[a[i].second] = m / (n - 1) + 1;
for (int i = 1; i <= n; i++)
cout << ans[i] << ' ';
return 0;
}
for (int i = 2; i <= n; i++)
a[i].first += a[i - 1].first;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= m; j++)
f[i][j] = oo;
f[0][m] = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j <= m; j++)
{
if (f[i][j] == oo)
continue;
for (int ii = i + 1; ii <= n; ii++)
for (int k = 1; k * (n - i) <= j; k++)
{
int jj = j - k * (n - i);
long long newF = f[i][j] + (a[ii].first - a[i].first) * (n - ii);
if (newF < f[ii][jj])
{
f[ii][jj] = newF;
tr[ii][jj] = {i, j};
}
}
}
cout << f[n][0] << endl;
for (int i = n, j = 0; i;)
{
int ii = tr[i][j].first, jj = tr[i][j].second;
for (int k = ii + 1; k <= n; k++)
at[k] += (jj - j) / (n - ii);
i = ii;
j = jj;
}
for (int i = 1; i <= n; i++)
ans[a[i].second] = at[i];
for (int i = 1; i <= n; i++)
cout << ans[i] << ' ';
}