#include #include #include #define INF 999999999 using namespace std; int tab[2005][15][25], nb[2005]; bool visited[2005][15][25]; int n, d; int trying(int at, int rest, int x) { if (at == n) return ((rest < 5) ? (rest) : (rest - 10)); if (x > d) return -INF; if (visited[at][rest][x]) return tab[at][rest][x]; visited[at][rest][x] = true; return tab[at][rest][x] = max(trying(at+1, (rest+nb[at])%10, x), trying(at, 0, x+1) + ((rest < 5) ? (rest) : (rest - 10))); } int main() { int s; int i, j, k; while (scanf (" %d %d", &n, &d) != EOF) { memset(visited, 0, sizeof(visited)); s = 0; for (i = 0; i < n; i++) { scanf (" %d", &nb[i]); s += nb[i]; } printf("%d\n", s-trying(0, 0, 0)); } return 0; }