Algorithms and Data Structures

  • Profound Academy

    • Status
      • 1
        Implementation
      • 2
        Bitwise operations
      • 3
        Prefix Sums
      • 4
        Sliding window / Two pointers
      • 5
        Modular Arithmetic
      • 6
        Number Theory
      • 7
        Binary Search
      • 8
        Basic Sorting
      • 9
        Greedy Algorithms
      • 10
        Basic Dynamic Programming
      • 11
        Recursion
      • 12
        Linked LIst
      • 13
        Queue & Stack
      • 14
        Binary tree + BST
      • 15
        Divide & Conquer + Advanced Sorting
      • 16
        Heap
      • 17
        Hashing
      • 18
        Graph Representation
      • 19
        BFS

  • Make heights uniform

    In an effort to optimize costs, the city design department has reached out to you to help them make all the buildings have the same height. All the buildings having uniform height will drastically reduce construction costs.
    They have prepared n buildings in the city that have different heights . Changing the height of a building by x, will result in a meeting that will last x minutes. As you’re busy, you’d like to minimize the time spent in meetings. What would be the minimal possible time spent in meetings?

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ ).
    The next line contains n space-separated integers (1 ≤ ) the initial heights of the buildings.

    Output

    The program should print a single integer - the minimal time spent in meetings.

    Examples

    Input
    Output
    5 1 4 8 2 9
    14

    Explanation

    The optimal height is 4 ⇒ we need to change 1 → 4 (3-minute meeting), 2 → 4 (2-minute meeting), 8 → 4 (4-minute meeting), 9 → 4 (5-minute meeting) ⇒ 14 minutes in total.
     

    Constraints

    Time limit: 1 seconds

    Memory limit: 512 MB

    Output limit: 1 MB

    To check your solution you need to sign in
    Sign in to continue