Given a list of prices, determine the minimum total cost after applying the usual discount rule to the cheapest item in each selected pair/grouping.
Sale
You are given prices of items in a shop. The task is to compute the minimum amount of money needed to buy all items when the shop applies a discount rule: among the items considered together, the cheapest one becomes free or gets the required discount according to the problem's sale logic.
For this problem, think of the key idea as identifying the cheapest eligible item(s) after sorting the prices and applying the discount rule to the items that matter. The goal is to produce the final total cost after the sale is applied to the full list.
Intuition
- Sort prices so the cheapest relevant items are easy to identify.
- Sum the remaining prices after the discount is applied.
- This is a straightforward implementation problem with sorting.
Input Format
- The first line contains an integer — the number of items.
- The second line contains integers describing the item prices.
Output Format
- Print one integer: the final total cost after applying the sale rule.
Constraints
- Prices are positive integers.
- The answer fits in a 64-bit signed integer.
Hints
- Sorting is usually the easiest way to reason about discount-style rules.
- Check which items are actually discounted and which are fully paid.
Input Format
- Integer .
- Array of item prices.
Output Format
- A single integer representing the final cost after the sale.
Constraints
- Prices are positive integers.
- Use 64-bit arithmetic for the sum.
Example 1
Input
5 5 1 3 4 2
Output
10
Explanation
After arranging the prices, apply the sale rule to the cheapest eligible items and sum the remaining paid prices.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.