Beach Clean-Up Scooper Total

Company: JPMorgan Chase

Difficulty: medium

Problem Statement

Beach Clean-Up Scooper Total Problem Description Stacey is coordinating a beach clean-up event with her university's Women in STEM Charity branch. The beach is covered with tin cans of varying weights arranged in a single line, indexed from `0` to `n - 1`. Stacey uses a scooper that can pick up three adjacent cans at a time. For each selection: She identifies the lightest remaining can, with weight `w`. She uses the scooper to pick up that can along with its two adjacent cans (or fewer if at the edge). She continues this process until there are no cans left on the beach. If multiple cans have the lightest weight, Stacey selects the one with the smallest index. If a can has fewer than two adjacent cans, she removes the available adjacent cans. After a selection, the cans that are still on the beach close the gap and form a single line again, so two cans that were separated only by cans just removed become adjacent to each other. Determine the sum of the weights of the lightest cans