#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void in(int **f, int n, int k)
{
cout << " n/t";
for (int i = 0; i < k; i++)
{
printf("%4d", i);
}
cout << endl;
for (int i = 0; i <= n; i++)
{
printf("%4d", i);
for (int t = 0; t < k; t++)
{
if (f[i][t] == INT_MAX)
{
cout << " +00";
}
else
{
printf("%4d", f[i][t]);
}
}
cout << endl;
}
}
int sub(int x, int y, int k)
{
int tmp = (x - y) % k;
return tmp >= 0 ? tmp : tmp + k;
}
int main()
{
int n, k, sum = 0;
cin >> n >> k;
int A[n];
for (int i = 0; i < n; i++)
{
cin >> A[i];
sum += A[i];
}
if (sum % k == 0)
{
cout << "Day da cho thoa man yeu cau." << endl
<< "Tong =" << sum;
return 0;
}
int **f = new int *[n + 1];
for (int i = 0; i < n + 1; i++)
{
f[i] = new int[k];
}
// optimize
f[0][0] = 0;
for (int t = 1; t < k; t++)
f[0][t] = INT_MAX;
for (int i = 1; i <= n; i++)
{
for (int t = 1; t < k; t++)
{
if (f[i - 1][t] < f[i - 1][sub(t, A[i - 1], k)] + 1)
f[i][t] = f[i - 1][t];
else
f[i][t] = f[i - 1][sub(t, A[i - 1], k)] + 1;
}
}
in(f, n, k);
cout << "Chieu dai day con: " << n - f[n][sum % k] << endl;
// truy vet
int t = sum % k;
sum = 0;
for (int i = n; i >= 1; i--)
{
if (f[i][t] == f[i - 1][t])
{
printf("a[%d]=%d;", i, A[i - 1]);
sum += A[i - 1];
}
else
{
t = sub(t, A[i - 1], k);
}
}
cout << endl
<< "Tong =" << sum;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2b2lkIGluKGludCAqKmYsIGludCBuLCBpbnQgaykKewogICAgY291dCA8PCAiIG4vdCI7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IGs7IGkrKykKICAgIHsKICAgICAgICBwcmludGYoIiU0ZCIsIGkpOwogICAgfQogICAgY291dCA8PCBlbmRsOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPD0gbjsgaSsrKQogICAgewogICAgICAgIHByaW50ZigiJTRkIiwgaSk7CiAgICAgICAgZm9yIChpbnQgdCA9IDA7IHQgPCBrOyB0KyspCiAgICAgICAgewogICAgICAgICAgICBpZiAoZltpXVt0XSA9PSBJTlRfTUFYKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBjb3V0IDw8ICIgKzAwIjsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHByaW50ZigiJTRkIiwgZltpXVt0XSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY291dCA8PCBlbmRsOwogICAgfQp9CgppbnQgc3ViKGludCB4LCBpbnQgeSwgaW50IGspCnsKICAgIGludCB0bXAgPSAoeCAtIHkpICUgazsKICAgIHJldHVybiB0bXAgPj0gMCA/IHRtcCA6IHRtcCArIGs7Cn0KCmludCBtYWluKCkKewogICAgaW50IG4sIGssIHN1bSA9IDA7CiAgICBjaW4gPj4gbiA+PiBrOwogICAgaW50IEFbbl07CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykKICAgIHsKICAgICAgICBjaW4gPj4gQVtpXTsKICAgICAgICBzdW0gKz0gQVtpXTsKICAgIH0KICAgIGlmIChzdW0gJSBrID09IDApCiAgICB7CiAgICAgICAgY291dCA8PCAiRGF5IGRhIGNobyB0aG9hIG1hbiB5ZXUgY2F1LiIgPDwgZW5kbAogICAgICAgICAgICAgPDwgIlRvbmcgPSIgPDwgc3VtOwogICAgICAgIHJldHVybiAwOwogICAgfQogICAgaW50ICoqZiA9IG5ldyBpbnQgKltuICsgMV07CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG4gKyAxOyBpKyspCiAgICB7CiAgICAgICAgZltpXSA9IG5ldyBpbnRba107CiAgICB9CiAgICAvLyBvcHRpbWl6ZQogICAgZlswXVswXSA9IDA7CiAgICBmb3IgKGludCB0ID0gMTsgdCA8IGs7IHQrKykKICAgICAgICBmWzBdW3RdID0gSU5UX01BWDsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47IGkrKykKICAgIHsKICAgICAgICBmb3IgKGludCB0ID0gMTsgdCA8IGs7IHQrKykKICAgICAgICB7CiAgICAgICAgICAgIGlmIChmW2kgLSAxXVt0XSA8IGZbaSAtIDFdW3N1Yih0LCBBW2kgLSAxXSwgayldICsgMSkKICAgICAgICAgICAgICAgIGZbaV1bdF0gPSBmW2kgLSAxXVt0XTsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgZltpXVt0XSA9IGZbaSAtIDFdW3N1Yih0LCBBW2kgLSAxXSwgayldICsgMTsKICAgICAgICB9CiAgICB9CiAgICBpbihmLCBuLCBrKTsKICAgIGNvdXQgPDwgIkNoaWV1IGRhaSBkYXkgY29uOiAiIDw8IG4gLSBmW25dW3N1bSAlIGtdIDw8IGVuZGw7CiAgICAvLyB0cnV5IHZldAogICAgaW50IHQgPSBzdW0gJSBrOwogICAgc3VtID0gMDsKICAgIGZvciAoaW50IGkgPSBuOyBpID49IDE7IGktLSkKICAgIHsKICAgICAgICBpZiAoZltpXVt0XSA9PSBmW2kgLSAxXVt0XSkKICAgICAgICB7CiAgICAgICAgICAgIHByaW50ZigiYVslZF09JWQ7IiwgaSwgQVtpIC0gMV0pOwogICAgICAgICAgICBzdW0gKz0gQVtpIC0gMV07CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIHQgPSBzdWIodCwgQVtpIC0gMV0sIGspOwogICAgICAgIH0KICAgIH0KICAgIGNvdXQgPDwgZW5kbAogICAgICAgICA8PCAiVG9uZyA9IiA8PCBzdW07CiAgICByZXR1cm4gMDsKfQ==