Sunday, 28 November 2021

Black Square Problem !!!

 PROBLEM 19: Black Square


Problem Reference : Codeforces 

Quite recently, a very smart student named Jury decided that lectures are boring, so he downloaded a game called "Black Square" on his super cool touchscreen phone.

In this game, the phone's screen is divided into four vertical strips. Each second, a black square appears on some of the strips. According to the rules of the game, Jury must use this second to touch the corresponding strip to make the square go away. As Jury is both smart and lazy, he counted that he wastes exactly ai calories on touching the i-th strip.

You've got a string s, describing the process of the game and numbers a1, a2, a3, a4. Calculate how many calories Jury needs to destroy all the squares?


Input:

The first line contains four space-separated integers a1a2a3a4 (0 ≤ a1, a2, a3, a4 ≤ 104).

The second line contains string s (1 ≤ |s| ≤ 105), where the і-th character of the string equals "1", if on the i-th second of the game the square appears on the first strip, "2", if it appears on the second strip, "3", if it appears on the third strip, "4", if it appears on the fourth strip.


Output:

Print a single integer — the total number of calories that Jury wastes.


Solution: 


  1. b1,b2,b3,b4 = input().split()
  2. a1 = int(b1)
  3. a2 = int(b2)
  4. a3 = int(b3)
  5. a4 = int(b4)
  6. x = input()
  7. sum1 = 0
  8. for i in range(0,len(x)):
  9. if x[i] == '1':
  10. sum1 = sum1 + a1
  11. elif x[i] == '2':
  12. sum1 = sum1 + a2
  13. elif x[i]== '3':
  14. sum1 = sum1 + a3
  15. else:
  16. sum1 = sum1 + a4
  17. print(sum1)


The above solution is in python language.


Examples
input
1 2 3 4
123214
output
13
input
1 5 3 2
11221
output
13


No comments:

Post a Comment

Rearrange an array with O(1) extra space

  PROBLEM 61:  Rearrange an array with O(1) extra space (For best view experience, view in windows version) Problem Reference : GeeksForGeek...