---
# System prepended metadata

title: 12138 - too many treasures
tags: [prefix sum]

---

# 12138 - too many treasures
>author: Utin
###### tags: `prefix sum`

---

## Brief
Search "prefix sum"

## Solution 0

```c=
#include <stdio.h>
#include <limits.h>

int n, q, flag = INT_MAX;
long long arr[1000001];

int main() {

    scanf("%d %d", &n, &q);

    for (int i = 1; i <= n; i++) {

        scanf("%lld", &arr[i]);

        // record the index of the first negative number
        if (arr[i] < 0 && flag == INT_MAX) flag = i;

        // prefix sum
        arr[i] += arr[i-1];
    }

    while (q--) {

        int l, r, m, amount = 0;
        scanf("%d %d %d", &l, &r, &m);

        // count the amount of the treasures
        if (flag > l) amount = m < flag-l ? m : flag-l;

        // output
        printf("%lld\n", arr[l+amount-1] - arr[l-1]);
    }
}

// By Utin
```

## Reference