题目传送门
解题思路:
f[i]表示i为末尾的最大组数.
AC代码:
1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 long long n,a[1001],sum,f[1001],num[1001]; 7 8 int main() { 9 scanf("%lld",&n); 10 for(int i = 1;i <= n; i++) { 11 scanf("%lld",&a[i]); 12 sum += a[i]; 13 num[i] = num[i-1] + a[i]; 14 } 15 if(sum < 0) { 16 printf("Impossible"); 17 return 0; 18 } 19 for(int i = 1;i <= n; i++) { 20 if(num[i] >= 0) 21 f[i] = 1; 22 for(int j = 0;j < i; j++) { 23 if(f[j] > 0 && num[i] - num[j] >= 0) 24 f[i] = max(f[i],f[j] + 1); 25 } 26 } 27 printf("%lld",f[n]); 28 29 return 0; 30 }