Description
Solutions
Submission
Find Niceness 🌺

In a tranquil village school, there are two students named Ramu and Sonu, each possessing a collection of N distinct chalks. Each student's chalks are of different lengths, represented by N positive integers. Ramu has arranged his collection of N chalks in a specific order based on their lengths. Sonu is eager to organize his own N chalks in a way that mimics Ramu's arrangement in terms of length changes i.e. if in Ramu's arrangement the kth chalk is bigger than the k+1th chalk, in Sonu's arrangement also the kth chalk will be bigger than the k+1th arrangement, alternately if it is smaller in Ramu's arrangement, then it will be smaller in Sonu's as well.

Sonu was busy arranging his chalks, when his teacher told him to also maximize the "niceness" of his arrangement. Here, the "niceness" of the arrangement is defined as the sum of the absolute length differences between all adjacent chalks in the arrangement.

Write a program to assist Sonu in achieving both the objectives: first, to mimic Ramu's length variation order, and second, to maximize the overall niceness of the arrangement.

Read the input from STDIN and print the output to STDOUT. Do not write arbitrary strings anywhere in the program, as these contribute to the standard output, and test cases will fail.

Objective

Find the maximum achievable niceness resulting from the arrangement of Sonu's chalk collection to mimic Ramu's chalk arrangement.

Example 1:

Input:  N = 4, a = [7, 1, 4, 9], b = [1, 2, 3, 4]
Output: 7
Explanation:
Given N=4. Ramu's chalks are arranged in the order of their lengths as 5 7 1 4 9, which corresponds to an increase decrease increase pattern of 2 1 3 4. Sonu has the chalk collection 1 2 3 4. To mimic Ramu's increase, decrease, Sonu can arrange his chalk in the following 5 ways: {1, 3, 2, 4} - niceness -> |1-3|+|3-2|+|2-4| = 2 + 1 + 2 = 5 {1, 4, 2, 3} - niceness -> |1-4|+|4-2|+|2-3| = 3 + 2 + 1 = 6 {2, 3, 1, 4} - niceness -> |2-3|+|3-1|+|1-4| = 1 + 2 + 3 = 6 {2, 4, 1, 3} - niceness -> |2-4|+|4-1|+|1-3| = 2 + 3 + 2 = 7 {3, 4, 1, 2} - niceness -> |3-4|+|4-1|+|1-2| = 1 + 3 + 1 = 5 As can be seen, the maximum niceness possible is 7, which is printed as output.
Constraints:
    Unknown for now 🐣
Thumbnail 0
Testcase

Result
Case 1

input:

output: