【81.82%】【codeforces 740B】Alyona and flowers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
【题目链接】:http://codeforces.com/contest/740/problem/B
【题解】
一开始被题目主体的分析搞晕了;
其实就是把所选择的区间里面的元素全部加在一起;
显然,如果一个区间里面;所有元素的和为负数。肯定不能选的;
因为选择以后答案只会减小;
因为 可能全都不选,所以答案可能为0;
【完整代码】
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 200;
LL n,m,a[MAXN],sum[MAXN];
int main()
{
// freopen("F:\\rush.txt","r",stdin);
cin >> n >> m;
for (int i = 1;i <= n;i++)
cin >> a[i];
for (int i = 1;i <= n;i++)
sum[i] = sum[i-1]+a[i];
LL ans = 0;
for (int i = 1;i <= m;i++)
{
int l,r;
cin >> l >> r;
int dd = sum[r]-sum[l-1];
if (dd >0)
ans += dd;
}
cout << ans << endl;
return 0;
}
【81.82%】【codeforces 740B】Alyona and flowers的更多相关文章
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【81.37%】【codeforces 734B】Anton and Digits
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 602D】Lipshitz Sequence
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
- 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【链表】【模拟】Codeforces 706E Working routine
题目链接: http://codeforces.com/problemset/problem/706/E 题目大意: 给一个N*M的矩阵,Q个操作,每次把两个同样大小的子矩阵交换,子矩阵左上角坐标分别 ...
- 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions
题目链接: http://codeforces.com/problemset/problem/710/D 题目大意: 两个等差数列a1x+b1和a2x+b2,求L到R区间内重叠的点有几个. 0 < ...
- 【动态规划】【最短路】Codeforces 710E Generate a String
题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...
随机推荐
- deep-in-es6(五)
解构 Destructuring: 解构赋值允许使用类似数组或对象字面量的语法将数组和对象的属性赋值给给中变量. 一般情况访问数组中的前三个元素: var first = arr[0]; var se ...
- 【canvas】跟随鼠标的星空连线
2019-01-23 19:57:38 挂一个比较简单的一个canvas应用,利用CPU进行粒子实时计算,直接面向过程写的 帧动画:浏览器在下一个动画帧安排一次网页重绘, requestAnimat ...
- js 判断是不是空、值是否存在
判断数组是否存在某个值: Array.indexOf(val) > -1 //存在 (缺陷:一是不够语义化,它的含义是找到参数值的第一个出现位置,所以要去比较是否不等于-1,表达起来不够直观.二 ...
- unity3d编程日志
2014/4/27 编写脚本的时候,加入了中文凝视,发现console面板有非常多不可思议的bug.查了一下发现是由于monodevelop脚本中文凝视报错,而英文凝视不会受影响. 解决方法:把凝视放 ...
- 史上最简单,js并获取手机型号
原先获取不了苹果系列的型号,但转换思路,先推断是否是苹果,再用分辨率获取型号 //获取手机型号函数begin function getPhoneType(){ //正则,忽略大写和小写 var pa ...
- screen-调节屏幕亮度
今天做项目的时候,需要实现一个功能,就是进入一个应用,在这个应用中,屏幕的亮度变为最亮.关键代码如下 bt1.setOnClickListener(new OnClickListener() { @O ...
- thinkphp内置标签简单讲解
thinkphp内置标签简单讲解 1.volist循环 name 需要遍历的数据 id 类似于foreach中 value offset 截取数据起始位置 length 截取数据的个数 mod 奇偶数 ...
- java中goto语句
goto是java中一个保留字,但在语言中并未使用它. goto语句起源于汇编语言的程序控制,是源码级上的跳跃,这使其招致了不好的声誉,若一个程序总是从一个地方跳转到另一个地方, 还有什么办法能识别程 ...
- 57.大数据线性处理csdn数据(fread,fwrite) 百万数据秒读数据
创建结构体存储csdn数据 struct csdn { ]; ]; ]; }; 对于分配的大小要先获取最大的长度,定义如下 //姓名最大长度 ; //密码最大长度 ; //邮件最大长度 ; //获取各 ...
- gomail发送附件
采用github.com/go-gomail/gomail/ 的邮件功能,可以发送附件 以及html文档,下面是其给出的demo,测试通过. package main //cmd: go get go ...