We need to understand problem: Given array a[1..n] (moods). Mother suggests m subarrays given by (l_i, r_i). For each subarray we can choose to take it or not. For each flower i (position), its contribution to happiness is a_i * (number of chosen subarrays that cover position i). Total happiness sum_i a_i * x_i, where x_i = number of chosen subarrays covering i. Since subarrays are independent, we can think of each subarray j contributes to each i in its interval a_i. So if we take a set S, total happiness = sum_{j in S} sum_{i=l_j}^{r_j} a_i.
Alternatively, we can think of for each subarray j we have a weight w_j = sum_{i=l_j}^{r_j} a_i. Then total happiness = sum_{j in S} w_j. So the problem reduces to: we can pick any subset of the m subarrays; the happiness is sum of their weights. But is that correct? Check example: a = [1,-2,1,3,-4]; subarrays: (1,2) weight = -1; (4,5) weight = -1; (3,4) weight = 4; (1,4) weight = 1+(-2)+1+3=3. If we pick subarrays (3,4) and (1,4) weights 4+3=7. That's output. If we pick all, total weight = -1-1+4+3=5? Actually -1-1+4+3=5, but earlier they computed if picking all? They didn't. Wait check: If we pick all, then each position's coverage: position1 in subarrays (1,2) and (1,4) -> count 2 -> contribution 2; pos2 in (1,2) and (1,4) -> 2 -> -4; pos3 in (3,4) and (1,4) -> 2 -> 2; pos4 in (3,4), (4,5), (1,4) -> 3 -> 9; pos5 in (4,5) -> 1 -> -4; total = 2-4+2+9-4=5. So that would be 5. But the problem states we can choose any subset to maximize total happiness. So we want maximize sum of weights of chosen subarrays. However, wait, is happiness indeed sum of weights? Let's derive: happiness = sum_i a_i * (number of subarrays covering i). Write as sum_{j in S} sum_{i in subarray j} a_i = sum_{j in S} w_j. So yes, it's exactly sum of the subarray sums. So the problem reduces to: Given m subarrays with weights (the sum of a over that interval), choose any subset to maximize total sum. Since we can pick any subset, we can simply pick all subarrays with positive weight, and ignore those with negative weight (or zero). Because subarrays are independent: taking a subarray adds its weight to total, not taking it adds nothing. There's no interaction or constraints (like each subarray can be chosen at most once, but they are independent). So the maximum happiness is sum_{j=1..m} max(0, w_j). That would be: For first example, weights: -1, -1, 4, 3 => positives: 4+3=7. matches. Second example: n=4, a=[1,2,3,4], subarrays: (1,3) weight=1+2+3=6, (2,4) weight=2+3+4=9, (1,1) weight=1 => all positive, sum=6+9+1=16, matches. Third example: a=[-1,-2], subarrays: (1,1) weight=-1, (1,2) weight=-3 => both negative, pick none gives 0. So answer is 0. matches. So solution: read n,m; read array a[1..n]; for each subarray read l,r; compute sum of a[l..r]; add if positive to answer. Output answer.
But wait, is there any twist? The statement: "Each subarray can encounter more than once." So duplicates allowed; we treat each occurrence separately. So yes.
Thus the solution is straightforward O(n*m) (or O(m * (r-l+1)) but constraints small. So implementation simple.
But be careful: sums can be negative, but we only add positive ones. The maximum could be zero if all negative.
We need to print integer, which can be up to maybe 100100100? n=100, each a_i up to 100, sum of subarray up to 10000, m=100 => max sum 1e6, fits int. Could be negative? But we take positive only.
Thus solution.
But I suspect there might be hidden trick: maybe there is interaction because choosing subarrays could affect x_i, but it's additive, no constraints. So simple.
Thus answer: compute ans = sum_{i=1..m} max(0, sum_{j=l_i}^{r_i} a_j).
Implement.
However, I recall some Codeforces problem: "Little Pony and Harmony Chest"? No. This is "Little Pony and Sort by Shift"? Actually it's Codeforces 358A? Let me recall: There's Codeforces problem "Little Pony and Sort by Shift"? Not. This problem is likely "Little Pony and Harmony Chest"? No. The actual problem: I think it's Codeforces #381 (Div2) or something? It's known as "Alyona and flowers"? Actually there is a problem "Alyona and flowers" (CF 740B?). Let's check: CF 740B - Alyona and flowers? Actually there is a problem: "Alyona and flowers" maybe. The description matches: "Alyona and flowers" where we pick subarrays from mother's suggestions. Yes, I think it's CF 740B. The solution is exactly as described. So it's straightforward.
Thus final answer: compute.
But double-check constraints: n,m up to 100, a_i between -100 and 100, so sums are manageable.
Edge cases: If no positive sums, answer 0.
Implementation in Python:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
m = int(next(it))
a = [0] + [int(next(it)) for _ in range(n)] # 1-indexed
ans = 0
for _ in range(m):
l = int(next(it))
r = int(next(it))
s = sum(a[l:r+1])
if s > 0:
ans += s
print(ans)
That's it.
We can also compute prefix sums to speed, but not needed.
Thus final. ```python
import sys
def main():
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
m = int(next(it))
# read moods, 1-indexed for convenience
a = [0] + [int(next(it)) for _ in range(n)]
ans = 0
for _ in range(m):
l = int(next(it))
r = int(next(it))
subarray_sum = sum(a[l:r+1])
if subarray_sum > 0:
ans += subarray_sum
print(ans)
if name == "main":
main()