Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. import com.sun.org.apache.xpath.internal.functions.FuncFalse;
  2.  
  3. import javax.rmi.ssl.SslRMIClientSocketFactory;
  4. import java.util.Scanner;
  5.  
  6. import static java.lang.Math.abs;
  7.  
  8. import java.util.Arrays;
  9.  
  10. public class MyProgram {
  11.     public static void main(String[] args) {
  12.         int[] input = {0,0};
  13.         Scanner in = new Scanner(System.in);
  14.         for (int i = 0; i < 2; i++)
  15.         {
  16.             input[i] = in.nextInt();
  17.         }
  18.         int N = input[0];
  19.         int K = input[1];
  20.         int[] nums = new int[N];
  21.         for (int i = 0; i < N; i++)
  22.         {
  23.             nums[i] = in.nextInt();
  24.         }
  25.         Arrays.sort(nums);
  26.         int f_index = 0;
  27.         int l_index = nums.length - 1;
  28.         int result = 1;
  29.         boolean negative = false;
  30.         boolean positive = false;
  31.         double mod1 = Math.pow(10,9);
  32.         long mod
  33.  
  34.         if (nums[nums.length - 1] < 0)
  35.         {
  36.             negative = true;
  37.         }
  38.         if (nums[nums.length - 1] > 0)
  39.         {
  40.             positive = true;
  41.         }
  42.  
  43.         if (K%2 != 0 && positive)
  44.         {
  45.             result *= nums[nums.length - 1];
  46.             l_index -= 1;
  47.             K -= 1;
  48.         }
  49.  
  50.         while (true)
  51.         {
  52.             if (K == 0)
  53.             {
  54.                 System.out.println(result);
  55.                 break;
  56.             }
  57.             else if (K == 1)
  58.             {
  59.                 result *= nums[l_index];
  60.                 result %= mod;
  61.                 K -= 1;
  62.             }
  63.  
  64.             else
  65.             {
  66.                 int minimal = nums[f_index] * nums[f_index + 1];
  67.                 int maximal = nums[l_index] * nums[l_index - 1];
  68.                 if (minimal > maximal && !negative)
  69.                 {
  70.                     result *= minimal;
  71.                     l_index += 2;
  72.                     K -= 2;
  73.                 }
  74.                 else if (minimal < maximal && negative)
  75.                 {
  76.                     result *= minimal;
  77.                     l_index += 2;
  78.                     K -= 2;
  79.                 }
  80.                 else
  81.                 {
  82.                     result *= maximal;
  83.                     l_index -= 2;
  84.                     K -= 2;
  85.                 }
  86.                 result %= mod;
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement