import java.io.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.Iterator;

import static java.lang.System.*;

class Ideone {
  // userResults variables are where we write output we want the user to see
  // it functions just like System.out

  // Readable -- via toString() -- version of userResults
  private OutputStream userResultsByteArray = new ByteArrayOutputStream();
  // Writable -- via println("") -- version of userResults
  private PrintStream userResults = new PrintStream(userResultsByteArray);
  // numberOfTests is incremented whenever a test case is evaluated
  private int numberOfTests = 0;
  // numberOfTestsPassed is incremented whenever a test case is evaluated successfully
  private int numberOfTestsPassed = 0;
  //(Integer) ((numberOfTestsPassed / numberOfTests)*100) == percent correct for student work

  // Class autorun entry point
  public static void main(String[] args) throws Exception, IOException {
    new Ideone().test();
  }

  // Put tests for specific template here
  private void test() throws Exception, IOException {

    List<String> input;
    List<String> output;

    // **** Add/edit test cases here ****
    input = Arrays.asList("34","\n","45","\n","55","\n","57","\n","79","\n","67","\n","68","\n","69","\n","72","\n","77","\n","88","\n","99","\n","101","\n","2345","\n","-10","\n","200","\n","200","\n","200","\n","200","\n","200","\n","-200","\n");
    output = Arrays.asList("Enter the values for the first array, up to 10000 values, enter a negative number to quit","","First Array", "34 45 55 57 79 67 68 69 72 77 88 99 101 2345", "Second Array", "200 200 200 200 200", "ERROR: Array not in correct order");
    assertRegex("Test:error", input, output);

    input = Arrays.asList("12","\n","23","\n","34","\n","45","\n","56","\n","67","\n","78","\n","89","\n","90","\n","-100","21","\n","32","\n","43","\n","54","\n","65","\n","76","\n","87","\n","98","\n","100","\n","-1","\n");
    output = Arrays.asList("Enter the values for the first array, up to 10000 values, enter a negative number to quit","","First Array", "12 23 34 45 56 67 78 89 90", "Second Array", "21 32 43 54 65 76 87 98 100", "12 21 23 32 34 43 45 54 56 65 67 76 78 87 89 90 98 100");
    assertRegex("Test:ok", input, output);

    generateScoreResults();

  }

  // Scoring Utilities
  private void generateScoreResults(){
    System.out.println("Your code has been evaluated against a set of test data.");
    System.out.format("You had %d out of %d tests pass correctly.\r\n",
        numberOfTestsPassed, numberOfTests);
    System.out.format("Your score is %d%%.\r\n", calculateScore());
    // Share any messages from assertions about pass/fail on specific tests
    System.out.print(userResultsByteArray.toString());
    System.out.format("Secrets: {\"score\": %d}\r\n", calculateScore());

  }

  private int calculateScore(){
    double passed = (double) numberOfTestsPassed;
    double taken = (double) numberOfTests;
    return (int) (( passed / taken )*100);
  }

  // Assertion/Testing Utilities

  // assertRegex will run the student's code, providing "inputs" as STDIN
  // It will regex match the output generated by the student's code
  // against the values provided in expectedList
  private void assertRegex(String testName, List<String> inputs, List<String> expectedList) throws Exception, IOException {
    // Preserve StdOut
    PrintStream origOut = System.out;
    // redirect StdOut
    ByteArrayOutputStream actuals = setStdOut();
    // Inject test data into StdIn
    setStdIn(inputs);
    // Run student code w/inputs
    Main.main(new String[0]);
    // Eval expected results against stub System.out (byteArray)
    Scanner scanner = new Scanner(actuals.toString());
    // Loop through actual results, and look for matches with expected results
    // Remove expected value once it is found
    userResults.println(testName);

    int numberOfTestCases = 1;
    try {
      for (int i = 0; i < expectedList.size(); i++){
        numberOfTests++;
        Boolean matched = false;
        String expected = expectedList.get(i);
        while (scanner.hasNextLine()){
          String actual = scanner.nextLine();
          if (actual.matches(".*"+expected+".*")) {
            numberOfTestsPassed++;
            matched = true;
            userResults.println("  Case #"+numberOfTestCases+" passed");
            // we replace the matched item with a regex /.\A/ which matches nothing
            // this is to prevent this expectation from matching another actual line of output
            expectedList.set(i, ".\\A");
            // break once we find a match for this test case, we don't want to match any more
            break;
          } // if (actual..
        } // while (scanner..
        if (!matched){
          userResults.println("  Case #"+numberOfTestCases+" failed");
        }
        // reset scanner for next operation
        scanner = new Scanner(actuals.toString());
        numberOfTestCases++;
      } // for (int i=...
    } finally {
      //restore StdOut
      System.setOut(origOut);
    }
  }

  // assertNotMatch will run the student's code, providing "inputs" as STDIN
  // It will make sure that NONE of the output values provided in expectedList are found in ANY of the actual output
  private void assertNotMatch(String testName, List<String> inputs, List<String> expectedList) throws Exception, IOException {
    // Print test name to user
    userResults.println(testName);
    // Preserve StdOut
    PrintStream origOut = System.out;
    // redirect StdOut
    ByteArrayOutputStream actuals = setStdOut();
    // Inject test data into StdIn
    setStdIn(inputs);
    // Run student code w/inputs
    Main.main(new String[0]);
    // Load test output into scanner
    Scanner scanner = new Scanner(actuals.toString());

    // Loop through test output (actuals), and look for matches with expected results
    // Fail test if a match is found
    int numberOfTestCases = 1;
    try {
      for (int i = 0; i < expectedList.size(); i++){
        numberOfTests++;
        Boolean passed = true;
        String expected = expectedList.get(i);
        while (scanner.hasNextLine()){
          String actual = scanner.nextLine();
          if (actual.matches(".*"+expected+".*")) {
            userResults.println("  Case #"+numberOfTestCases+" failed");
            passed = false;
            // break b/c this test case has failed and no further lines need to be examined
            break;
          } // if (actual..
        } // while (scanner..
        if (passed){
          numberOfTestsPassed++;
          userResults.println("  Case #"+numberOfTestCases+" passed");
        }
        // reset scanner for next operation
        scanner = new Scanner(actuals.toString());
        numberOfTestCases++;
      } // for (int i=...
    } finally {
      //restore StdOut
      System.setOut(origOut);
    }
  }


  //Standard In/Out Utilities

  // Sets StdOut to a new ByteStream
  // Returns the ByteStream so whatever is written
  // can be read back out later via this object
  private ByteArrayOutputStream setStdOut(){
    ByteArrayOutputStream newStdOutByteArray = new ByteArrayOutputStream();
    PrintStream newStdOut = new PrintStream(newStdOutByteArray);
    System.setOut(newStdOut);
    return newStdOutByteArray;
  }

  // set System.in/STDIN to string: newIn
  // Note: be sure to preserve System.in before calling, if you need it later
  // You can preserve System.in simply with InputStream oldIn = System.in;
  private void setStdIn(List<String> listIn){
    String strIn = joinCR(listIn);
    ByteArrayInputStream stubIn = new ByteArrayInputStream(strIn.getBytes());
    System.setIn(stubIn);
  }

  // Basic Utilities
  //joins string list together w/carriage returns
  private String joinCR(List<String> list){
    return join(list, '\n');
  }

  //join a list into a delimted string
  private String join(List<String> list, char delimiter){
    StringBuilder sb = new StringBuilder();
    for(String s: list) {
       sb.append(s).append(delimiter);
    }
    sb.deleteCharAt(sb.length()-1);
    return sb.toString();
  }

}//Ideone class

// Following line needs to be uncommented and system swaps in student code
// in its place when run within GCB by code-runner system

//$$YIELD$$

class Main {
  /*
   While Loops
   Tracing Code and Counting the Number of Iterations
   More Loops
   Technique - flag variables
   Strings as class types (vs primitive)
   */
	  //WildCard
    //assume only one * per string

	 public static void main (String str[]) throws IOException {

         Scanner scan = new Scanner (System.in);

         //length will be less than 10,000
         //count how many elements entered into each array
         int a1 = 0;
         int a2 = 0;
         int ar1[] = new int [10000];
         int ar2[] = new int [10000];

         for (int i=0; i< ar1.length; i++)
         {
              ar1[i] = Integer.MAX_VALUE;
              ar2[i] = Integer.MAX_VALUE;
         }

         int val =1;
         System.out.println("\n\nEnter the values for the first array, up to 10000 values, enter a negative number to quit");
         while (val >= 0)
         {
              val = scan.nextInt();
              if (val >= 0)
              {
                   ar1[a1] = val;
                   a1++;
              }
         }



         //enter 2nd array
         val =1;
         System.out.println("\n\nEnter the values for the second array, up to 10000 values, enter a negative number to quit");
         while (val >= 0)
         {
              val = scan.nextInt();
              if (val >= 0)
              {
                   ar2[a2] = val;
                   a2++;
              }
         }

         System.out.println("\n\nFirst Array: ");
         for (int i =0; i < a1; i++)
              System.out.print(ar1[i] + " ");

         System.out.println("\n\nSecond Array: ");
         for (int i =0; i < a2; i++)
              System.out.print(ar2[i] + " ");




         //check if in order
         boolean inOrder = true;

         for (int i = 1; i < a1; i++)
         {
              if (ar1[i-1] > ar1[i])
                   inOrder = false;
         }
         for (int i = 1; i < a2; i++)
         {
              if (ar2[i-1] > ar2[i])
                   inOrder = false;
         }
//         System.out.println("\n\nIn order?? " + inOrder);


         if (!inOrder)
         {
              System.out.println("\n\nERROR: Array not in correct order");
         }
         else
         {

              //merge and output
              int merge [] = new int [a1 + a2];
              //System.out.println("\n\nLength of new array: " + merge.length);
              a1 = 0;
              a2 = 0;
              for (int i = 0; i < merge.length; i++)
              {
                   if (ar1[a1] <= ar2[a2])
                   {
                        merge[i] = ar1[a1];
                        a1++;
                   }
                   else
                   {
                        merge[i] = ar2[a2];
                        a2++;
                   }
              }
              System.out.println("\n\nMerged Array: ");
              for (int i =0; i < merge.length; i++)
                   System.out.print(merge[i] + " ");
         }
     }

}