您的位置:首页 > 其它

B. Alyona and flowers

2017-02-19 22:24 190 查看
time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Little Alyona is celebrating Happy Birthday! Her mother has an array of n flowers. Each flower has some mood, the mood of i-th
flower is ai.
The mood can be positive, zero or negative.

Let's define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona wants to choose several of the subarrays suggested by her mother. After that, each of the flowers will add to the girl's happiness its mood multiplied
by the number of chosen subarrays the flower is in.

For example, consider the case when the mother has 5 flowers, and their moods are equal to 1,  - 2, 1, 3,  - 4.
Suppose the mother suggested subarrays (1,  - 2), (3,  - 4), (1, 3), (1,  - 2, 1, 3).
Then if the girl chooses the third and the fourth subarrays then:

the first flower adds 1·1 = 1 to the girl's happiness, because he is in one of chosen subarrays,

the second flower adds ( - 2)·1 =  - 2, because he is in one of chosen subarrays,

the third flower adds 1·2 = 2, because he is in two of chosen subarrays,

the fourth flower adds 3·2 = 6, because he is in two of chosen subarrays,

the fifth flower adds ( - 4)·0 = 0, because he is in no chosen subarrays.

Thus, in total 1 + ( - 2) + 2 + 6 + 0 = 7 is added to the girl's happiness. Alyona wants to choose such subarrays from those suggested by the
mother that the value added to her happiness would be as large as possible. Help her do this!

Alyona can choose any number of the subarrays, even 0 or all suggested by her mother.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 100) —
the number of flowers and the number of subarrays suggested by the mother.

The second line contains the flowers moods — n integers a1, a2, ..., an ( - 100 ≤ ai ≤ 100).

The next m lines contain the description of the subarrays suggested by the mother. The i-th
of these lines contain two integers li and ri(1 ≤ li ≤ ri ≤ n)
denoting the subarray a[li], a[li + 1], ..., a[ri].

Each subarray can encounter more than once.

Output

Print single integer — the maximum possible value added to the Alyona's happiness.

Examples

input
5 4
1 -2 1 3 -4
1 2
4 5
3 4
1 4


output
7


input
4 3
1 2 3 4
1 3
2 4
1 1


output
16


input
2 2
-1 -2
1 1
1 2


output
0


Note

The first example is the situation described in the statements.

In the second example Alyona should choose all subarrays.

The third example has answer 0 because Alyona can choose none of the subarrays.

解题说明:此题是一道模拟题,给出N个数字ai,M组(left,right),对于每组(left,right)可以选择要或者不要,如果要,则记录下标(left,right)之间的数出现次数加一,求出最终(ai*ai出现的次数)的最大和。此题可以采用贪心算法,对于每组(left,right)假设对应数字均只出现一次,则只要他们的和大于等于0,那就选择要。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;

int main()
{
int n,m;
int l,r,sum,ans=0;
int i,j;
int a[110];
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=m;i++)
{
scanf("%d%d",&l,&r);
for(j=l,sum=0;j<=r;j++)
{
sum+=a[j];
}
if(sum>0)
{
ans+=sum;
}
}
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: