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. <转>思考力是万力之源

    做任何事情,要懂得积累和总结 每天进步一点点,一年365天下来就不得了   来自为知笔记(Wiz)

  2. 网站统计中的数据收集原理及实现(share)

    转载自:http://blog.codinglabs.org/articles/how-web-analytics-data-collection-system-work.html 网站数据统计分析工 ...

  3. Vim速查脑图

  4. 创建SO bapi

    转自http://blog.sina.com.cn/s/blog_1647b3eff0102wi32.html 1.创建销售订单使用的BAPI BAPI_SALESORDER_CREATEFROMDA ...

  5. PEP 8

    官方文档: PEP 8 :Style Guide for Python Code 部分翻译: http://www.blogjava.net/lincode/archive/2011/02/02/34 ...

  6. Ecstore会员密码加密方式破解

    <?php //以下是加密方式,亲测有效 $string_md5 = md5(md5("密码")."用户名"."注册时间");//三个 ...

  7. UVA213 信息解码

    对于lrj的想法,真是太清晰.自己真的是很难去做到.贴此代码,加以练习,加以感悟. #include<cstdio> #include<cstring> ][<<] ...

  8. Http中 Post和 Get的区别

    1.表面上的区别 1.GET在浏览器回退时,是无害的,而Post会再次提交请求 2.Get产生的Url地址会被Bookmark,而Post不会 3.Get请求会被浏览器主动Cache,而Post不会, ...

  9. 嵌入式系统上实现GPS全球定位功能

    GPS(Global Positioning System)即全球定位系统,是由美国建立的一个卫星导航定位系统,利用该系统,用户可以在全球范围内实现全天候.连续.实时的三维导航定位和测速:另外,利用该 ...

  10. BarTender是怎么做出雪花状文字

    一些小伙伴在做标签时,发现有的人做的标签上的文字颜色不是纯色的,问我是怎么做的.这种雪花状文字要设置出来其实很简单,只要用到字体颜色填充工具就可以了.下面,小编就来给大家简单介绍一下BarTender ...