Codeforces#381(Div. 2) B.Alyona and flowers【思维+暴力】

来源:互联网 发布:ubuntu镜像文件安装 编辑:程序博客网 时间:2024/06/07 13:34

B. Alyona and flowers
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 to1,  - 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 andm (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 integersa1, a2, ..., an ( - 100 ≤ ai ≤ 100).

The next m lines contain the description of the subarrays suggested by the mother. Thei-th of these lines contain two integers li and ri (1 ≤ li ≤ ri ≤ n) denoting the subarraya[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 41 -2 1 3 -41 24 53 41 4
Output
7
Input
4 31 2 3 41 32 41 1
Output
16
Input
2 2-1 -21 11 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的序列,其中有m段可选择的子序列,对应我们可以任意选择几个或者不选子序列,选择出来的总价值为:

这个数被选择的次数*本身这个数的价值的累加和。

求如何选择能够使得最终这个总价值最大。


思路:


1、脑筋急转弯的题,题目描述是想将你带入一个坑:求最终的总价值是如何求的。

其实我们只要考虑一下,我们不需要去探讨最终每个数的贡献值到底为多少,我们考虑每个区间到底能够贡献出多少价值即可。


2、那么我们对应求出每个可供选择的子序列的价值即可,每个子序列的价值就是区间内所有数字的和,那么如果对应当前子段的和大于了0,那么我们就有将其选择的必要。相反,如果当前子段的和小于等于0,我们就不去选择这一段。


3、那么根据上述分析过程之后,直接累加子段和大于0的子区间即可。


Ac代码:

#include<stdio.h>#include<string.h>using namespace std;int a[1005];int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        int output=0;        for(int i=0;i<m;i++)        {            int x,y;            scanf("%d%d",&x,&y);            int cnt=0;            for(int j=x;j<=y;j++)            {                cnt+=a[j];            }            if(cnt>0)output+=cnt;        }        printf("%d\n",output);    }}






0 0
原创粉丝点击