#P11301. Minimum Operations to Unlock the Vault

    ID: 13377 Type: Default 1000ms 256MiB

Minimum Operations to Unlock the Vault

Minimum Operations to Unlock the Vault

In this problem, you are given a screen showing N digits, represented by an array \(C\), and a target password represented by another array \(P\). The displayed digits are transformed into the password digits by performing a sequence of operations. Each operation works on a contiguous segment (from index \(i\) to \(j\) with \(1 \leq i \leq j \leq N\)) and increments every digit in that segment by 1 modulo \(K+1\).

The operation is defined as follows:

  • Choose two integers \(i\) and \(j\) such that \(1 \leq i \leq j \leq N\).
  • For every index \(l\) with \(i \leq l \leq j\), update \(C_l\) to \((C_l + 1) \bmod (K+1)\).

Your task is to determine the minimum number of operations required to convert the array \(C\) into the password array \(P\>.

Hint: For each position \(i\), compute the number of increments needed as \(d_i = (P_i - C_i + (K+1)) \bmod (K+1)\). The minimum number of operations is given by:

[ \text{moves} = d_1 + \sum_{i=2}^{N}{\max(0, d_i - d_{i-1})} ]

inputFormat

The first line contains two integers: \(N\) (the number of digits) and \(K\) (the modulo parameter such that the modulo is \(K+1\)).

The second line contains \(N\) integers representing the array \(C\) (the currently displayed digits).

The third line contains \(N\) integers representing the array \(P\) (the password digits).

outputFormat

Output a single integer, which is the minimum number of operations required to transform the array \(C\) into the array \(P\).

sample

3 4
0 1 2
1 2 3
1