Mr. Young's Picture Permutations

#include <iostream>
#include <cstring>

using namespace std;

const int N = 31;
typedef long long LL;

int n;
int s[10];
LL f[N][N][N][N][N];//光看样例应该也能猜到会爆int
//f[s1][s2][s3][s4][s5]表示第i排放了第si个人时的合影方案数量

int main()
{
    while (~scanf("%d", &n) && n)
    {
        memset(s, 0, sizeof s);
        for (int i = 0; i < n; i ++) cin >> s[i];
        
        memset(f, 0, sizeof f);
        f[0][0][0][0][0] = 1;
        for (int a = 0; a <= s[0]; a ++)
            for (int b = 0; b <= min(a, s[1]); b ++)
                for (int c = 0; c <= min(b, s[2]); c ++)
                    for (int d = 0; d <= min(c, s[3]); d ++)
                        for (int e = 0; e <= min(d, s[4]); e ++)
                        {
                            LL &t = f[a][b][c][d][e];
                            if (a && a - 1 >= b) t += f[a - 1][b][c][d][e];
                            if (b && b - 1 >= c) t += f[a][b - 1][c][d][e];
                            if (c && c - 1 >= d) t += f[a][b][c - 1][d][e];
                            if (d && d - 1 >= e) t += f[a][b][c][d - 1][e];
                            if (e) t += f[a][b][c][d][e - 1];
                        }
        
        cout << f[s[0]][s[1]][s[2]][s[3]][s[4]] << endl;
    }
    
    return 0;
}