#include <stdio.h> #include <string.h> #define MIN(X,Y) ((X)<(Y) ? (X) : (Y)) int n,c[1010],r[1010][10][1010],dp[10][300][1010]; int solve(int aver,int c,int b,int k); int main(void) { //freopen("cards.in","r",stdin); //freopen("cards.out","w",stdout); scanf("%d",&n); int i,j,k; for(i=1;i<=n;i++) scanf("%d",&c[i]); memset(r,-1,sizeof(r)); int s; for(i=1;i<=n;i++) for(j=1;j<=8;j++) { r[i][j][s=0]=i-1; for(k=i;k<=n;k++) if(c[k]==j) r[i][j][++s]=k; } int min=1,max=n,m,t; while(min<max) { m=(min+max+1)/2; memset(dp,-1,sizeof(dp)); if(solve(m/8,m%8,0,1)<=n) min=m; else max=m-1; } printf("%d\n",min); return 0; } int solve(int aver,int c,int b,int k) { if(dp[c][b][k]>=0) return dp[c][b][k]; int t=(1<<8)-1; if( (t&b)==t && !c ) return dp[c][b][k]=k-1; else if((t&b)==t) return dp[c][b][k]=n+1; dp[c][b][k]=n+1; int i; for(i=0;i<8;i++) if((b&(1<<i))==0) { if(r[k][i+1][aver]>=0) dp[c][b][k]=MIN(dp[c][b][k],solve(aver,c,b|(1<<i),r[k][i+1][aver]+1)); if( c && r[k][i+1][aver+1]>=0 ) dp[c][b][k]=MIN(dp[c][b][k],solve(aver,c-1,b|(1<<i),r[k][i+1][aver+1]+1)); } return dp[c][b][k]; }