import java.io.*; import java.util.*; import java.awt.geom.*; /** * Solution to Beautiful Mountains * * Stack blocks a prime distance apart. ALL stacks must be a prime distance * from ALL other stacks, NOT just adjacent stacks! * * Well, there are only 6 possibilities: * Case 1: All the blocks are in a single stack * Case 2: The blocks are in exactl3 2 stacks, 2 apart. * Case P: The blocks are in exactly 2 stacks, a prime number P distance apart, P>2. * Case 2P: The blocks are in three stacks: the first two 2 apart, and the last one a prime distance from there, * where prime+2 is also prime. * Case P2: The blocks are in three stacks: the first a prime apart, and the last one 2 from there, * where prime+2 is also prime. * Case 2P2: The blocks are in four stacks, distance 2, then a prime, then 2, * where prime+2 and prime+4 are also prime. * * There can't be any other combos. Consider P1>2 and P2>2, both prime. * Then P1 and P2 are both odd, so P1+P2 is even, and thus not prime. * * @author vanb */ public class mountains_vanb { public Scanner sc; public PrintStream ps; /** * Driver. * @throws Exception */ public void doit() throws Exception { sc = new Scanner( System.in ); //new File( "mountains.judge" ) ); ps = System.out; //new PrintStream( new FileOutputStream( "mountains.vanb" ) ); // List of primes int primes[] = new int[30000]; // Number of primes int np = 0; // Use the Sieve of Eratosthenes to get primes boolean isprime[] = new boolean[31000]; Arrays.fill( isprime, true ); isprime[0] = false; isprime[1] = false; for( int i=2; ii) to i long rightcosts[] = new long[30001]; for(;;) { int n = sc.nextInt(); if( n==0 ) break; // Compute mountains, sums, leftcosts mountains[0] = sc.nextInt(); sums[0] = mountains[0]; leftcosts[0] = 0; for( int i=1; i=0; i-- ) { rightcosts[i] = rightcosts[i+1] + sums[n-1] - sums[i]; } long best = Long.MAX_VALUE; for( int i=0; i2 for( int j=1; primes[j]1 && isprime[p+2] ) { // Case 2P long cost2P = costP - leftcosts[i] + leftcosts[i-2] + mountains[i-1]; if( cost2P1 && i+p