import java.util.*; public class Seq_bob_wrongans { public static Scanner in; public static int n; public static int rolls[]; public static int grid[][]; public static void main(String[] args) { in = new Scanner(System.in); n = in.nextInt(); rolls = new int[n+1]; for (int i = 1; i <= n; i++) { // skip 0--leave it for initial condition rolls[i] = in.nextInt(); } grid = new int[14][n+1]; // extra row and col for initial conditions for (int row = 1; row <= 13; row++) { int bound = Math.min(n - 65 + 5*row,row*15); // leave at least 65-5*row dice // Earliest we can compute row entry is after at least 5*row dice rolled for (int col = 5*row; col <= bound; col++) { int max = 0; // now maximize over # of extra rolls taken to get here. We can // use at most 15 dice, so we could go back up to 14 columns // from the current one. // (e.g., xrolls=0 means we rolled only 5 dice, xrolls=1 means // we re-rolled one of them, ...) int lowb = 5*row-5; for (int xrolls = 0; xrolls <= 10; xrolls++) { if (col-5-xrolls < lowb) break; // outside bounds int score = bestof(row,col-4-xrolls,col) + grid[row-1][col-5-xrolls]; if (score > max) max = score; } //System.err.println("Row,col "+row+","+col+": best score = " + max); grid[row][col] = max; } } int ans = 0; // Highest score among all the row 13 entries for (int i = 65; i <= n; i++) { if (grid[13][i] >= ans) ans = grid[13][i]; } System.out.println(ans); } public static int bestof(int row, int startcol, int endcol) { if (endcol-startcol+1 < 5) System.out.println("too few dice"); if (endcol-startcol+1 >15) System.out.println("too many dice"); if (row <= 6) return row*count(row,startcol,endcol); if (row == 7) return ofakind(3,startcol,endcol); if (row == 8) return ofakind(4,startcol,endcol); if (row == 9) return fullhouse(startcol,endcol); if (row == 10) return straight(4,startcol,endcol); if (row == 11) return straight(5,startcol,endcol); if (row == 12) return chance(startcol,endcol); return yahtzee(startcol,endcol); } // returns number of occurrences of goal between roll #s s and e (max 5): public static int count(int goal, int s, int e) { int ans = 0; for (int i = s; i <= e; i++) if (rolls[i]==goal) ans++; return Math.min(5,ans); } public static int ofakind(int k, int s, int e) { int sum = 0; int temp[] = Arrays.copyOfRange(rolls,s,e+1); int sz = temp.length; Arrays.sort(temp); int count[] = new int[7]; int loc= 16; for (int i = sz-1; i >= 0; i--) { count[temp[i]]++; if (count[temp[i]] >= k) { loc = i; break; } } if (loc == 16) return 0; sum = 3*temp[loc]; if (k==3) { if (loc == sz-3) sum += temp[loc-1]+temp[loc-2]; else if (loc == sz-4) sum += temp[sz-1]+temp[loc-1]; else sum += temp[sz-1]+temp[sz-2]; return sum; } sum += temp[loc]; // one more if (loc == sz-4) sum += temp[loc-1]; else sum += temp[sz-1]; return sum; } public static int fullhouse(int s, int e) { int count[] = new int[7]; int two = 0, three = 0; for (int i = s; i <= e; i++) { count[rolls[i]]++; } for (int i = 1; i <= 6; i++) { if (count[i]>=2) two++; if (count[i]>=3) three++; } if (two>=2 && three>=1) return 25; else return 0; } public static int straight(int k, int s, int e) { int count[] = new int[7]; for (int i = s; i <= e; i++) { count[rolls[i]]++; } int consec = 1; int bestconsec = 0; for (int i = 2; i <= 6; i++) { if (count[i] > 0 && count[i-1] > 0) { consec++; if (consec > bestconsec) bestconsec = consec; } else consec = 1; } if (k==4 && bestconsec >= 4) return 30; if (k==5 && bestconsec >= 5) return 40; return 0; } public static int chance(int s, int e) { int temp[] = Arrays.copyOfRange(rolls,s,e+1); int sz = temp.length; Arrays.sort(temp); int sum = 0; for (int i = sz-5; i <= sz-1; i++) sum += temp[i]; return sum; } public static int yahtzee(int s, int e) { int count[] = new int[7]; for (int i = s; i <= e; i++) { count[rolls[i]]++; if (count[rolls[i]] >= 5) return 50; } return 0; } }