public class Mutalisk {
int n;
int[] hp;
int[][][] dp;
boolean check(int limit)
{
dp = new int[n+1][limit+1][limit+1];
for(int i = 0; i <= n; i++)
for(int j = 0; j <= limit; j++)
for(int k = 0; k <= limit; k++)
dp[i][j][k] = -1;
dp[0][limit][limit] = limit;
for(int i = 0; i < n; i++)
for(int j = 0; j <= limit; j++)
for(int k = 0; k <= limit; k++)
if(dp[i][j][k] >= 0)
{
int n9 = j;
int n3 = k;
int n1 = dp[i][j][k];
//System.out.println(i + " -> " + n9 + " " + n3 + " " + n1);
for(int useN9 = 0; useN9 <= n9; useN9 ++)
{
if((useN9-1) * 9 >= hp[i]) break;
for(int useN3 = 0; useN3 <= n3; useN3 ++)
{
if(useN3 > 0 && useN9*9 + (useN3-1)*3 >= hp[i]) break;
if(useN3 + useN9 > limit) break;
int useN1
= Math.
max(0, hp
[i
] - useN9
*9 - useN3
*3); if(useN1 > n1) continue;
if(useN1 + useN3 + useN9 > limit) continue;
dp
[i
+1][n9
-useN9
][n3
-useN3
] = Math.
max(dp
[i
+1][n9
-useN9
][n3
-useN3
], n1
- useN1
); if(i+1 == n)
return true;
}
}
}
return false;
}
public int minimalAttacks(int[] x)
{
n = x.length;
hp = x;
int L = 0, R = 7 * x.length, M;
while(R-L > 1)
{
M = (L+R) / 2;
if(check(M))
R = M;
else
L = M;
}
return R;
}
}
cHVibGljIGNsYXNzIE11dGFsaXNrIHsKCgogICAgaW50IG47CiAgICBpbnRbXSBocDsKICAgIGludFtdW11bXSBkcDsKCiAgICBib29sZWFuIGNoZWNrKGludCBsaW1pdCkKICAgIHsKICAgICAgICBkcCA9IG5ldyBpbnRbbisxXVtsaW1pdCsxXVtsaW1pdCsxXTsKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDw9IG47IGkrKykKICAgICAgICAgICAgZm9yKGludCBqID0gMDsgaiA8PSBsaW1pdDsgaisrKQogICAgICAgICAgICAgICAgZm9yKGludCBrID0gMDsgayA8PSBsaW1pdDsgaysrKQogICAgICAgICAgICAgICAgICAgIGRwW2ldW2pdW2tdID0gLTE7CiAgICAgICAgZHBbMF1bbGltaXRdW2xpbWl0XSA9IGxpbWl0OwogICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgICAgIGZvcihpbnQgaiA9IDA7IGogPD0gbGltaXQ7IGorKykKICAgICAgICAgICAgICAgIGZvcihpbnQgayA9IDA7IGsgPD0gbGltaXQ7IGsrKykKICAgICAgICAgICAgICAgICAgICBpZihkcFtpXVtqXVtrXSA+PSAwKQogICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgaW50IG45ID0gajsKICAgICAgICAgICAgICAgICAgICAgICAgaW50IG4zID0gazsKICAgICAgICAgICAgICAgICAgICAgICAgaW50IG4xID0gZHBbaV1bal1ba107CiAgICAgICAgICAgICAgICAgICAgICAgIC8vU3lzdGVtLm91dC5wcmludGxuKGkgKyAiIC0+ICIgKyBuOSArICIgIiArIG4zICsgIiAiICsgbjEpOwogICAgICAgICAgICAgICAgICAgICAgICBmb3IoaW50IHVzZU45ID0gMDsgdXNlTjkgPD0gbjk7IHVzZU45ICsrKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZigodXNlTjktMSkgKiA5ID49IGhwW2ldKSBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZvcihpbnQgdXNlTjMgPSAwOyB1c2VOMyA8PSBuMzsgdXNlTjMgKyspCiAgICAgICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYodXNlTjMgPiAwICYmIHVzZU45KjkgKyAodXNlTjMtMSkqMyA+PSBocFtpXSkgYnJlYWs7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYodXNlTjMgKyB1c2VOOSA+IGxpbWl0KSBicmVhazsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpbnQgdXNlTjEgPSBNYXRoLm1heCgwLCBocFtpXSAtIHVzZU45KjkgLSB1c2VOMyozKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZih1c2VOMSA+IG4xKSBjb250aW51ZTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZih1c2VOMSArIHVzZU4zICsgdXNlTjkgPiBsaW1pdCkgY29udGludWU7CgoKCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZHBbaSsxXVtuOS11c2VOOV1bbjMtdXNlTjNdID0gTWF0aC5tYXgoZHBbaSsxXVtuOS11c2VOOV1bbjMtdXNlTjNdLCBuMSAtIHVzZU4xKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZihpKzEgPT0gbikKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIHRydWU7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgfQoKICAgIHB1YmxpYyBpbnQgbWluaW1hbEF0dGFja3MoaW50W10geCkKICAgIHsKICAgICAgICBuID0geC5sZW5ndGg7CiAgICAgICAgaHAgPSB4OwogICAgICAgIGludCBMID0gMCwgUiA9IDcgKiB4Lmxlbmd0aCwgTTsKICAgICAgICB3aGlsZShSLUwgPiAxKQogICAgICAgIHsKICAgICAgICAgICAgTSA9IChMK1IpIC8gMjsKICAgICAgICAgICAgaWYoY2hlY2soTSkpCiAgICAgICAgICAgICAgICBSID0gTTsKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgTCA9IE07CiAgICAgICAgfQogICAgICAgIHJldHVybiBSOwogICAgfQoKfQo=