CodeChef PESU ECC Chapter
Problem of the Day·July 20, 2026·Archive →

Chef's Candy Distribution

Medium
GreedyArrays1s limit· 256 MB

Set by PESUECC Problem Setters

Rules

How the Arena works · Rules

Scoring, fair play, and what gets you disqualified.

  1. 1One Problem of the Day. Read the statement on the left and solve it in the browser editor on the right.
  2. 2Points are awarded by speed: the faster your first Accepted submission, the higher you rank on the daily board (1000 for 1st down to 100 for 10th and beyond).
  3. 3The on-screen timer is only indicative — your official solve time is recorded on the server the moment your submission is Accepted.
  4. 4Only your first Accepted submission counts toward the leaderboard.
  5. 5Copy, paste and right-click are disabled inside the arena. Switching tabs or windows and taking screen captures during a live solve are recorded for review — each counts as one integrity flag.
  6. 6Accumulating more than 5 integrity flags (blocked paste/copy attempts, tab/window switches, screen captures) removes you from that day's top 10 — an accepted solve then earns only the 100-point base.
  7. 7The problem statement can't be selected or copied, it's watermarked with your identity, and it blurs when you leave the tab — leaked problems are traceable back to you.
  8. 8Write your own code. Plagiarism, sharing solutions, or automating submissions leads to disqualification and forfeiting the bounty.
  9. 9Don't probe or exploit the judge, and don't attempt to extract the hidden test cases.
Problem01

Chef is handing out candies to n children standing in a line. Each child has a rating based on how well they solved today's warm-up puzzle. Chef must give out candies under two rules:

  1. Every child gets at least one candy.
  2. Any child with a strictly higher rating than an immediate neighbour must receive strictly more candies than that neighbour.

Chef is generous but not wasteful — help him find the minimum total number of candies he needs to distribute.

Input Format

  • The first line contains a single integer n — the number of children.
  • The second line contains n space-separated integers r[1], r[2], …, r[n] — the ratings of the children in line order.

Output Format

Print a single integer: the minimum number of candies Chef must distribute.

Constraints

  • 1 ≤ n ≤ 10^5
  • 0 ≤ r[i] ≤ 10^9

Sample

Input
3
1 0 2
Output
5

Explanation

One optimal distribution is [2, 1, 2]:

  • The first child (rating 1) outranks the second (rating 0), so they need more than 1 candy → 2.
  • The third child (rating 2) outranks the second, so they also need 2.

The total is 2 + 1 + 2 = 5, and no valid distribution uses fewer candies.

main.cpp
00:00
Loading editor…
Custom inputusing sample

Run uses this input. Submit always judges the hidden tests.

Console

Write your solution, then Run it against the sample or Submit to the judge's hidden tests. Faster accepted solves earn more of the daily bounty.

Your submissions

0 this session

No submissions yet — hit Submit to send your solution to the judge.

Speed Bounty

Points by finish order — the faster you get accepted, the more you earn.

1st
1000
pts
2nd
800
pts
3rd
600
pts
4th
500
pts
5th
400
pts
6th
300
pts
7th
250
pts
8th
200
pts
9th
150
pts
10th+
100
base
Live Standings
0 solved today

Loading…