Codeforces 677A Vanya and Fence Solution & Explanation

Difficulty : 800

Problem Description

Vanya and his friends are walking along the fence of height h and they do not want the guard to notice them. In order to achieve this the height of each of the friends should not exceed h. If the height of some person is greater than h he can bend down and then he surely won’t be noticed by the guard. The height of the i-th person is equal to ai.

Consider the width of the person walking as usual to be equal to 1, while the width of the bent person is equal to 2. Friends want to talk to each other while walking, so they would like to walk in a single row. What is the minimum width of the road, such that friends can walk in a row and remain unattended by the guard?

Input

The first line of the input contains two integers n and h (1 ≤ n ≤ 1000, 1 ≤ h ≤ 1000) — the number of friends and the height of the fence, respectively.

The second line contains n integers ai (1 ≤ ai ≤ 2h), the i-th of them is equal to the height of the i-th person.

Output

Print a single integer — the minimum possible valid width of the road.

Eamples

Input3 7
4 5 14
Output4
Input6 1
1 1 1 1 1 1
Output6
Input6 5
7 6 8 9 10 5
Output11

Understand this problem in 3 seconds:

The first line contains two integers, the first one is the number of friends, and the second one is the height of the fence.

The second line input contains the height of each friend.

And if the friend is greater than or equal to the fence, it will take 2 width, otherwise, it will take 1. Count the total width it needs for all the friends.


Solution

By using an iteration of all the friends, we can easily calculate the width we need.

C# Solution

Solution1

string[]arr = Console.ReadLine().Split(' '); //split by space
//int n = int.Parse(arr[0]); //number of friends >> unused
int h = int.Parse(arr[1]); //height of the fence


string[]arr2 = Console.ReadLine().Split(' '); //split by space

int width = 0;

foreach(var item in arr2){
    var tmp = int.Parse(item);
    width += tmp>h ? 2 : 1;
}

Console.WriteLine(width);

In this solution, we don’t need the first integer, so we remark it.

We use a foreach loop to check height of each friend and if they are higher than the fence. If the friend is taller than the fence, increase width by 2, else increase by 1.

Java Solution

Solution1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] arr = scanner.nextLine().split(" ");
        
        int n = Integer.parseInt(arr[0]);    
        int h = Integer.parseInt(arr[1]);  
        
        
        String[]arr2 = scanner.nextLine().split(" "); //split by space

        int width = 0;
        
        for(String friendHeight : arr2){
            int tmp = Integer.parseInt(friendHeight);
            width += tmp>h ? 2 : 1;
        }
        
        System.out.println(width);
    }
}

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 677A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *