DP 아닌가... 


long long solution(int index) //index부터 시작한 최소값 구하기

{

if (index >= n + 1)

return 0;


long long& ret = dp[index];

if (ret != -1) return ret;


ret = MAXNUM;


int sum = man[index];

for (int next = index+1; (sum <= w&&next<=n+1); next++)

{

ret = min(ret, max((long long)(w - sum)*(w - sum), solution(next)));

sum += man[next];

}


return ret;

}


이러니깐 시간초과 뜨던데,,ㅠㅠ