We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home
  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Functional Programming
  3. Memoization and DP
  4. Bangalore Bank

Bangalore Bank

Problem
Submissions
Leaderboard
Discussions
Editorial

There is a famous old bank in Bangalore. It has just started the process of filling its database with bank account numbers of its clients. In order to put one account number to the database, an employee has to insert it from a piece of paper to a computer using a standard keyboard (without using number pad found on the right hand side of most keyboards). The weird thing is that every employee assigned to this task can type in using only 2 index fingers (one left hand index finger and one right hand index finger).

Below is the sample representation of number keys present in the keyboard.

1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - 0

He can perform any one of the following steps:

  1. He can move any one of his fingers to adjacent keys.
  2. He can press the key just below any of his fingers. But he can press only one key at a time.

Each of the above steps takes 1 second. So moving a finger from key 3 to key 5 takes 2s, moving a finger from key 7 to key 2 takes 5s, and moving a finger from key 0 to key 8 takes 2s (Key 0 is the rightmost key). Similarly, pressing a single key takes 1 second.

Write a program that computes the minimal time needed to add account number of an employee to the database. Before the process, an employee can place his finger wherever he wants. All digits should be inserted in the given order.

Note

  • It is not necessary that left finger will always lie on the left side of right finger. They can also lie on the same key, and in opposite direction also.

Input
In the first line, there is a number n denoting the length of the bank account number.
In the second line, there are n digits separated by a single space denoting the bank account number.

Output
In one and only line, print the minimum time (in seconds) required to rewrite the bank account number according to the above rules.

Constraints
1 ≤ n ≤ 104

Input #00

2
1 2

Output #00

2

Input #01

3
1 0 3

Output #01

5

Explanations

Test Case #00: An employee can put his left finger on key 1 and his right finger on key 2 before the process, so the whole process takes 2 seconds.

Test Case #01: An employee can put his left finger on key 1 and his right finger on key 0 before the process. From that position, it takes 2 seconds to press first two keys. After that, he can move his left finger from key 1 to key 3, which takes 2 seconds and then press it which takes additional second. The whole process takes 5 seconds. Note that key 0 is the rightmost key.


Tested by Ray Williams Robinson Valiente, abhiranjan

Author

pkacprzak

Difficulty

Hard

Max Score

50

Submitted By

450

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Helpdesk
  • Careers
  • Terms Of Service
  • Privacy Policy

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website

Join us

Create a HackerRank account

Be part of a 26 million-strong community of developers

Please signup or login in order to view this challenge

or
Already have an account?Log in