ZOJ 3447 Doraemon & amp; #39; s Number Game (Java priority queue & amp; #183; BigInteger)

Source: Internet
Author: User
Tags rounds

ZOJ 3447 Doraemon & #39; s Number Game (Java priority queue & #183; BigInteger)

You can delete the number of 2 to k and add the product of the deleted number to the original array until only one number is left to calculate the maximum and minimum values. difference

The smaller the number of values selected, the less the number of values selected. The larger the result is, the larger the result is. Because this large number can be multiplied by the maximum number of multiples, the maximum number of times of the + 1 operation is. obviously, it is conducive to increasing the final result.

In the same way, the larger the number of items selected, the more items selected, and the smaller the final result.

In this way, the maximum value is to take the smallest two numbers at a time. The maximum k number is obtained at a time. It is easy to use the priority queue to implement the result. It is more convenient to use the BigInteger in Java.

 

Import java. math. bigInteger; import java. util. *; public class Main {public static void main (String args []) {role in = new role (System. in); int N = 105; PriorityQueue
 
  
Inc = new PriorityQueue
  
   
(); // The default priority of the priority queue is small. The priority is the PriorityQueue of the incremental queue.
   
    
Dec = new PriorityQueue
    
     
(N, new Comparator
     
      
() {Public int compare (BigInteger o1, BigInteger o2) {return-o1.compareTo (o2) ;}}); // implement the Comparator interface in descending order with a greater priority of int n, k; BigInteger a, B, one = BigInteger. ONE; List
      
        List = new ArrayList
       
         (); While (in. hasNext () {n = in. nextInt (); k = in. nextInt (); list. clear (); for (int I = 0; I <n; ++ I) {a = in. nextBigInteger (); list. add (a);} inc. addAll (list); while (inc. size ()> 1) {a = inc. poll (); B = inc. poll (); inc. add (. multiply (B ). add (one);} dec. addAll (list); while (dec. size ()> 1) {a = one; for (int I = 0; I <k; ++ I) if (dec. size ()> 0) a =. multiply (dec. poll (); a =. add (one); dec. add (a);} System. out. println (inc. poll (). subtract (dec. poll ();} in. close ();}}
       
      
     
    
   
  
 

 

ZOJ Problem Set-3447 Doraemon's Number Game Time Limit: 2 Seconds Memory Limit: 65536 KB

Doraemon and Nobita are playing a number game. First, Doraemon will give NobitaNPositive numbers. Then Nobita can deal with these numbers for two rounds. Every time Nobita can deleteI(2 ≤IK) Numbers from the number set. Assume that the numbers deleted is a [1], a [2]... a [I]. There shocould be a new numberX= (A [1] * a [2] *... * a [I] + 1) to be inserted back into the number set. the operation will be applied to the number set over and over until there remains only one number in the set. the number is the result of round. assume two resultsAAndBAre produced after two rounds. Nobita can get |A-B| Scores.

Now Nobita wants to get the highest score. Please help him.

Input

Input will contain no more than 10 cases. The first line of each case contains two positive integersNAndK(1 ≤N≤ 100, 1 ≤K≤ 50). The following line containsNPositive integers no larger than 50, indicating the numbers given by Doraemon.

Output

For each case, you shocould output highest score in one line.

Sample Input
6 31 3 4 10 7 15
Sample Output
5563
Hint

For most cases,N≤ 20




 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.