Algorithm


Problem Name: 2 AD-HOC - beecrowd | 2449

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2449

Door Lock

 

By OBI - Olimpíada Brasileira de Informática 2014 BR Brazil

Timelimit: 1

Joaozinho was coming home one day when he realized he had lost the key to the door. Desperate, he decided to ask for help to his friend Roberto, who in a few seconds managed to open the door using their tools.

Amazed at the speed at which his friend managed to open the door of his house without a key, he decided to ask him how he had managed it. Roberto explained that Joaozinho's house lock is based on a system of different sizes of pins, once aligned in the same height M, enable the door opening.

A lock is a set of N pins arranged horizontally which can be moved up or down with the aid of a metal key which, when inserted in the lock, can increase or decrease by 1 mm, while the height of any two consecutive pins.

Joaozinho as a perfectionist exemplary decided to release its lock on fewer movements where each movement is to choose two consecutive pins of the lock and increase or decrease the height of the two pins at 1mm. After all the pins having height exactly equal to M, the lock is unlocked.

 

Input

 

The first line entry contains two integers N (1 ≤ N ≤ 1000) and M (1 ≤ M ≤ 100) representing, respectively, the amount of the lock pin and the height they should be for the lock to be unlocked.

The second line contains the entry N (1 ≤ Ni ≤ 100) integers representing the heights of the lock pin.

 

Output

 

Its program to print a line containing an integer representing the minimum amount of movement to unlock the lock.

 

 

 

Input Sample Output Sample

4 50

45 45 55 55

10

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
int main() {
    int n, tamanho, vetor[1010], resposta = 0;
    scanf("%d %d", &n, &tamanho);
    for (int i = 0; i  <  n; i++) scanf("%d", &vetor[i]);
    for (int i = 0; i  <  n; i++) {
        int diferenca = tamanho - vetor[i];
        resposta += abs(diferenca);
        vetor[i + 1] += diferenca;
    }
    printf("%d\n", resposta);
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 50 45 45 55 55

Output

x
+
cmd
10
Advertisements

Demonstration


Previous
#2448 Beecrowd Online Judge Solution 2448 Postman Solution in C, C++, Java, Js and Python
Next
#2450 Beecrowd Online Judge Solution 2450 Matrix Ladder Solution in C, C++, Java, Js and Python