#C11236. Taco Race
Taco Race
Taco Race
You are given a circular race track divided into $N$ sections. Each section i has a positive distance denoted by $D_i$. Starting from section $S$ (1-indexed), determine whether there exists a contiguous segment of the track (following the circular order) whose total distance is exactly $M$ meters.
Formally, let the circular sequence be \[ A = [D_S, D_{S+1}, \dots, D_N, D_1, \dots, D_{S-1}], \] find if there exist indices \(1 \leq i \leq j \leq N\) such that \[ \sum_{k=i}^{j} A_k = M. \]
If such a contiguous segment exists, output YES
; otherwise, output NO
.
inputFormat
The input is given via standard input (stdin) and consists of two lines:
- The first line contains three space-separated integers: $N$ (the number of sections), $M$ (the total distance to achieve), and $S$ (the starting section, 1-indexed).
- The second line contains $N$ space-separated integers:
D1 D2 ... DN
, where eachDi
represents the distance of the i-th section.
outputFormat
Output a single line to standard output (stdout) containing the string YES
if there exists a contiguous segment (in circular order) whose sum is exactly $M$; otherwise, output NO
.
5 10 3
1 2 3 4 5
YES