# Problem Description
This problem involves stacking a sequence of numbered cards in descending order (largest number on top) from a given horizontal arrangement. The goal is to calculate the total effort required to achieve this vertical stack. The effort is calculated based on the distance moved and the weight of the stack being moved.

# Visual Facts
1. **Initial Setup**: Cards are arranged horizontally in a sequence.
2. **Final Goal**: Cards must be stacked vertically in descending order (largest number on top).
3. **Distance**: The horizontal distance moved by the cards or stacks during each step.
4. **Effort**: Calculated as the product of the weight of the stack being moved and the distance moved.
5. **Weight**: The current step number.
6. **Steps**: Each step involves moving a card or a stack of cards to form a larger stack.
7. **Examples**:
   Initial sequence: [2] [3] [1] [4]
   Step 1: [2, 1] [3] [] [4]
   Explanation: The "1" moves below "2", and the distance moved is 2. weight = step_idx = 1, effort += weight * distance = 1 * 2 = 2
   Step 2: [] [3, 2, 1] [] [4]
   Explanation: The stack [2, 1] moves below "3", and the distance moved is 1. weight = step_idx = 2, effort += weight * distance = 1 * 2 = 4
   Step 3: [] [] [] [4, 3, 2, 1]
   Explanation: The stack [3, 2, 1] moves below "4", and the distance moved is 2.  weight = step_idx = 3, effort += weight * distance = 2 * 3 = 10
   Result: All numbers are stacked vertically in descending order: [4, 3, 2, 1]. The total effort is 10.

# Visual Patterns
1. **Movement Rules**:
   - Cards can only be stacked in descending order.
   - Moves can involve a single card or an already-formed stack.
   - Each move must result in a valid descending order stack.
   
2. **Distance Calculation**:
   - Distance is the number of positions crossed horizontally.
   - The distance is the absolute difference between the current position and the target position index.

3. **Effort Calculation**:
   - Weight is the current step number.
   - Effort for each move is calculated as the product of the weight and the distance moved.
   - For example, if it's the first time moving the cards, the weight is 1; if it's the third time moving the cards, the weight is 3.

4. **Step to solve**:
   1. Identify the smallest card，this card will be the first one to move, and its corresponding index is current_position = cards.index(min(cards)).
   2. Then, begin iterating through cards, with number in range(1, len(cards) + 1).
   3. Find the index of the current card being traversed: index = cards.index(num).
   4. Calculate the distance: distance = abs(index - current_position).
   5. Add the calculated distance to the total effort: effort += distance * (num-1).
   6. Update the current_position to the new position after moving the card: current_position = index.

# Solution
def solution(cards: list[int]) -> int:
    """
    Based on the visual examples provided, calculate the total effort required to sequentially stack the given cards.

    Input:
    - cards: A list of integers representing a sequence of cards. (len(cards) >= 1)

    Output:
    - An integer representing the total effort required to fully stack the cards in descending order.
    """
    effort = 0
    current_position = cards.index(min(cards))
    for num in range(1, len(cards) + 1):
        index = cards.index(num)
        s = abs(index - current_position)
        effort += s*(num-1)
        current_position = index
    return effort


# Test Script
def test():
    test_cases = [
        ([2, 3, 1, 4], 10),
        ([1, 2, 3, 4], 6),
        ([4, 3, 2, 1], 6),
        ([3, 1, 4, 2], 14),
        ([1], 0),
        ([2, 1], 1),
        ([15, 2, 9, 5, 13, 6, 7, 12, 1, 4, 3, 10, 11, 14, 8], 701),
        ([5, 3, 2, 1, 4, 6], 53),
        ([15, 2, 9, 5, 4, 6, 7, 12, 1, 13, 3, 10, 11, 14, 8], 619),
    ]
    for cards, expected_output in test_cases:
        original_cards = cards[:] # Save original input
        
        try:
            output = solution(cards)
        except Exception as e:
            error_message = (
                f"An exception occurred while running the test case:\n"
                f"`cards`: {original_cards}\n\n"
                f"Exception: '{str(e)}'"
            )
            assert False, error_message

        if output != expected_output:
            error_message = (
                f"The following Test Case failed:\n"
                f"`cards`: {original_cards}\n\n"
                f"Actual Output: {output}\n"
                f"Expected Output: {expected_output}"
            )
            assert False, error_message

test()