Under construction

Still being built. Things may change

Ah-Choo!

Interview

Time limit1sMemory limit512 MB

Summary
Compute the least Dynamic Time Warping distance between two equal-length integer sequences, where every point must match at least one point of the other and matches cannot cross.
Level

Medium5 of 10

Topics
Dynamic programming, Array, Implementation
Solved
No attempts yet

Problem

Every person has a distinct sneeze. Loud sneezes and short cute sneezes mix with different voices, so each person produces a distinct waveform.

Dongi studies data science in graduate school and wants to test whether sneezes identify people. With graduation near, Dongi starts the experiment at once for the thesis. Jiae and Jisu, whose sneezes sound alike, join as subjects.

To decide whether two sneezes come from the same person, Dongi needs a similarity score. The score uses DTW (Dynamic Time Warping).

A sound waveform is time series data with time on the xx axis. Even one person sneezing again produces a slightly different waveform each time. A direct comparison at equal times reports low similarity for similar shapes. DTW first matches each time point of one waveform to the closest point of the other waveform and then measures the error. This matching absorbs shifts from sneezing speed and recording start.

Write the heights of sounds XX and YY at times ii and jj as X(i)X(i) and Y(j)Y(j). Matching point ii of XX with point jj of YY costs (X(i)−Y(j))2(X(i)-Y(j))^2. Every point of XX matches at least one point of YY, and every point of YY matches at least one point of XX. The sum over matched pairs is the distance, and the inverse of the least DTW distance is the similarity.

Matches keep order. If ii of XX matches jj of YY and a later i′i' of XX matches j′j' of YY, then j≤j′j \le j' holds. Crossed matches are invalid. Matching second 7 of XX to second 6 of YY and then second 8 of XX to second 5 of YY is an example of a forbidden match.

Take X=[10,20,45,20,14,15]X = [10, 20, 45, 20, 14, 15] and Y=[10,25,50,50,30,15]Y = [10, 25, 50, 50, 30, 15]. Matching equal times gives 0+25+25+900+256+0=12060+25+25+900+256+0=1206. A better order-preserving match gives 0+25+25+25+100+1+0=1760+25+25+25+100+1+0=176. Matches with a crossing do not count.

Given two recorded sneezes as sequences in time order, write a program that computes the least DTW distance.

Input

The first line gives a natural number NN, the length of each waveform. The second line gives NN integers in time order, the heights of the first waveform XX, separated by spaces. The third line gives the heights of the second waveform YY in the same format.

Every height is an integer with 0≤h≤10000 \le h \le 1000.

Output

Print the least distance between the two waveforms as an integer.

Examples3

  1. Example 1

    Input
    2
    0 100
    100 0
    
    Expected output
    20000
    
  2. Example 2

    Input
    6
    10 20 45 20 14 15
    10 25 50 50 30 15
    
    Expected output
    176
    
  3. Example 3

    Input
    10
    38 22 56 51 18 42 16 34 5 41
    32 52 10 19 42 17 25 49 43 31
    
    Expected output
    868