Alyona and flowers

CodeForces - 740B

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.

sol:这题意TMD居然看成了一道数据结构题(智减inf)
搞出前缀和,显然如果是正数就算入答案,否则不算
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,m,a[N],Qzh[N];
int S[N];
int main()
{
int i,j,ans=;
R(n); R(m);
for(i=;i<=n;i++) R(a[i]);
for(i=;i<=n;i++) Qzh[i]=Qzh[i-]+a[i];
for(i=;i<=m;i++)
{
int l=read(),r=read();
if(Qzh[r]-Qzh[l-]>) ans+=(Qzh[r]-Qzh[l-]);
}
Wl(ans);
return ;
}
/*
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
*/
 

codeforces740B的更多相关文章

  1. CodeForces-740B Alyona and flowers

    题目要求选择一些花的集合,如果暴力去枚举每种选择方法明显是不行的.换种方式考虑:每一个集合都能为最后的答案做出要么正的.要么负的.要么0贡献,先把所有集合能做出的贡献预处理,然后从m个集合里面选择贡献 ...

随机推荐

  1. Java多线程(六)——线程让步

    一.yield()介绍 yield()的作用是让步.它能让当前线程由“运行状态”进入到“就绪状态”,从而让其它具有相同优先级的等待线程获取执行权:但是,并不能保证在当前线程调用yield()之后,其它 ...

  2. for或者while的标记循环

    for或者while的标记循环 今天在写代码的时候,发现一个for循环前有一个字母,不知道这个是什么语法,后来查了一下,这个语法是用来实现标记循环的功能 这个是代码块 r:for(int rowNum ...

  3. SQLSERVER操作字段约束,修改字段名称等

    -- 表加注释EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'注释内容' , @level0type=N'SCHEM ...

  4. asp.net调用前台js调用后台代码分享

    asp.net调用前台js调用后台代码分享 C#前台js调用后台代码前台js<script type="text/javascript" language="jav ...

  5. java中scanner的正确用法

    Scanner s = new Scanner(System.in); int choice = 0; if(s.hasNextInt()) { choice = s.nextInt(); } s.c ...

  6. 原生jS之-去掉字符串开头和结尾的空字符

    怎么解决这个问题?? 思路就是我们利用正则匹配到所谓的空格,然后替换为空字符,我们要用到的是str的replace API 代码如下: <!DOCTYPE html> <html l ...

  7. iOS基于B站的IJKPlayer框架的流媒体探究

    阅读数:6555 学习交流及技术讨论可新浪微博关注:极客James 一.流媒体 流媒体技术从传输形式上可以分为:渐进式下载和实施流媒体. 1.渐进式下载 它是介于实时播放和本地播放之间的一种播放方式, ...

  8. jmeter的jtl日志转html报告常见报错笔记

    问题:生成的jmeter文件可以放任意位置 输入命令转换hmtl报告 PS D:\user\80003288\桌面\Ques> jmeter -g .\test1.jtl -e -o .\rep ...

  9. python内涵段子爬取练习

    # -*- coding:utf-8 -*-from urllib import request as urllib2import re# 利用正则表达式爬取内涵段子url = r'http://ww ...

  10. Oracle转换函数

    ()--转换函数 --数字转换字符串 )||'分' from dual; ||'' from dual; ()--日期转字符串 select to_char(sysdate,'yyyy-mm-dd') ...