B. Alyona and flowers

Problem Description:

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.

Sample Input:

5 4

1 -2 1 3 -4

1 2

4 5

3 4

1 4

Sample Output:

7

【题目链接】B. Alyona and flowers

【题目类型】

&题意:

给m个区间,你要选几个使得区间内的元素乘区间选择的次数最大,问这个最大值是多少?

&题解:

模拟遍样例,再加仔细想想,发现只有这个区间所有数加起来>0的时候才要选,并且对答案的贡献也就是>0的这个数,所有把符合区间的这个数相加就好了。

【时间复杂度】O(\(n^2\))

&代码:

#include <bits/stdc++.h>
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d %d",&(N),&(M))
#define rez(i,a,b) for(int i=(a);i<=(b);i++)
const int maxn = 100 + 9 ;
int n, m, a[maxn], u, v, res;
int main() {
while (~SII(n, m)) {
res = 0;
rez(i, 1, n) SI(a[i]);
rez(i, 1, m) {
SII(u, v);
int ans = 0;
rez(j, u, v) {
ans += a[j];
}
res += ans > 0 ? ans : 0;
}
printf("%d\n", res);
}
return 0;
}

Codeforces Round #381 (Div. 2)B. Alyona and flowers(水题)的更多相关文章

  1. Codeforces Round #358 (Div. 2) A. Alyona and Numbers 水题

    A. Alyona and Numbers 题目连接: http://www.codeforces.com/contest/682/problem/A Description After finish ...

  2. Codeforces Round #261 (Div. 2) B. Pashmak and Flowers 水题

    题目链接:http://codeforces.com/problemset/problem/459/B 题意: 给出n支花,每支花都有一个漂亮值.挑选最大和最小漂亮值得两支花,问他们的差值为多少,并且 ...

  3. Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题

    Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  4. Codeforces Round #290 (Div. 2) A. Fox And Snake 水题

    A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...

  5. Codeforces Round #322 (Div. 2) A. Vasya the Hipster 水题

    A. Vasya the Hipster Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/p ...

  6. Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题

    B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...

  7. Codeforces Round #368 (Div. 2) A. Brain's Photos 水题

    A. Brain's Photos 题目连接: http://www.codeforces.com/contest/707/problem/A Description Small, but very ...

  8. Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题

    A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...

  9. Codeforces Round #355 (Div. 2) A. Vanya and Fence 水题

    A. Vanya and Fence 题目连接: http://www.codeforces.com/contest/677/problem/A Description Vanya and his f ...

随机推荐

  1. 如何执行字符串的PHP代码

    如何执行字符串的PHP代码 最近因项目需要,引出一个议题:如何执行字符串的php代码(php和html混写). 注:传统情况下,php代码存储在文件中,直接运行文件即可.以下讨论的情况是,如果php代 ...

  2. 使用js实现input输入框的增加

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 黑马----JAVA内部类

    黑马程序员:Java培训.Android培训.iOS培训..Net培训 黑马程序员--JAVA内部类 一.内部类分为显式内部类和匿名内部类. 二.显式内部类 1.即显式声明的内部类,它有类名. 2.显 ...

  4. C#异常语句

    try: 用于检查发生的异常,并帮助发送任何可能的异常. catch: 以控制权更大的方式处理错误,可以有多个catch子句. finally :无论是否引发了异常,finally的代码块都将被执行. ...

  5. 烧写ARM开发板系统教程----->uboot 、内核以及文件系统

    一.sd启动 将u-boot镜像写入SD卡,将SD卡通过读卡器接上电脑(或直接插入笔记本卡槽),通过"cat /proc/partitions"找出SD卡对应的设备,我的设备节点是 ...

  6. Apache commons-client authentication(授权)

    import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.UsernamePasswo ...

  7. linux 查找文件与进程常用命令

    Linux的五个查找命令 1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> ...

  8. 如何使用花生壳 发布WCF服务 进行外网访问

    当我们发布WCF服务的时候,可以直接通过服务器的域名或者IP进行. 但是如果仅仅是通过花生壳进行域名解析,需要我们自己在设置的时候注意以下几点, 直接用图说明问题 1.首先配置花生壳,在红色处填写一个 ...

  9. JavaScript:修改作用域外变量

    今天在看JavaScript学习指南的时候做的课后习题,也因此详细的对函数的传入参数进行比较深入的研究. 题目如下: 函数如何才能修改其作用域之外的变量?编写一个函数,由1~5的数字组成的数组作为参数 ...

  10. ns115 step by step

    一,安装环境: sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev lib ...