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 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.

分析:维护前缀和,若询问区间和>0,则对答案有贡献;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define intxt freopen("in.txt","r",stdin)
const int maxn=1e5+;
using namespace std;
int gcd(int p,int q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,a[maxn],sum[maxn];
int main()
{
int i,j;
scanf("%d%d",&n,&m);
rep(i,,n)scanf("%d",&a[i]),sum[i]=sum[i-]+a[i];
ll ans=;
rep(i,,m)
{
int l,r;
scanf("%d%d",&l,&r);
if(sum[r]-sum[l-]>)ans+=sum[r]-sum[l-];
}
printf("%lld\n",ans);
//system("Pause");
return ;
}

Alyona and flowers的更多相关文章

  1. Codeforces Round #381 (Div. 2)B. Alyona and flowers(水题)

    B. Alyona and flowers Problem Description: Let's define a subarray as a segment of consecutive flowe ...

  2. 【81.82%】【codeforces 740B】Alyona and flowers

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. CodeForces-740B Alyona and flowers

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

  4. CF740B Alyona and flowers 题解

    Content 有 \(n\) 个数 \(a_1,a_2,a_3,...,a_n\),给定 \(m\) 个区间,你可以选择一些区间使得它们的总和最大(也可以不选),求这个最大的总和. 数据范围:\(1 ...

  5. Codeforces Round #381 (Div. 2) A B C 水 构造

    A. Alyona and copybooks time limit per test 1 second memory limit per test 256 megabytes input stand ...

  6. Codeforces Round #381 (Div. 2) 复习倍增//

    刷了这套题  感触良多 我想 感觉上的差一点就是差很多吧 . 每次都差一点  就是差很多了... 不能气馁..要更加努力去填补那一点点.  老天不是在造物弄人,而是希望你用更好的自己去迎接自己. A. ...

  7. CODEFORCES ROUND #740 ANALYSES BY TEAM:RED & BLACK

    A.Alyona and copybooks Problems: 给你一个数n和代价分别为a, b, c.数量不限的1, 2, 3,求将n凑成4的倍数的最小代价 Analysis: cj:取个模随便凑 ...

  8. codeforces740B

    Alyona and flowers CodeForces - 740B Little Alyona is celebrating Happy Birthday! Her mother has an ...

  9. CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)

    Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...

随机推荐

  1. [M]表格中的天正文字转换问题

    若表格中含有天正文字,则不能使用MagicTable直接转换,需要先EXPLODE命令分解(快捷键为x),天正单行文字和天正多行文字都可以使用该命令分解为普通AutoCAD单行文字,分解后即可正常转换 ...

  2. 从excel读数据到informix的Found a quote for which there is no matching quote错误

    我从excel读取数据,然后存储到Informix数据库里.偶尔会发现出现Found a quote for which there is no matching quote这个错误.调试后发现,是因 ...

  3. SURF 特征法

    public static void FindMatch(Mat modelImage, Mat observedImage, out long matchTime, out VectorOfKeyP ...

  4. Cocoapod安装使用和常见问题(转载)

      1.cocoapod的按照,cocoapod是运行在ruby环境下的,在ruby环境的 ,像cocoapod这样的开源项目时放在放在rubygems服务器上面的,但国内访问https://ruby ...

  5. URL中文参数乱码的一个解决办法

      浏览器对有中文参数的url大部分都用utf-8编码,但我也曾经遇见过用GB2312编码的:如果遇见这样情况,那么接受到的参数就会出现乱码.乱码情况视服务器解码方式,Asp.Net网站一般默认为ut ...

  6. Hibernate之环境搭建

    开始之前,我想先理清一个概念,即ORM是什么? ORM介绍 全称:Object/Relation Mapping,即对象/关系映射. ORM也可以理解为一种规范,具体的ORM框架可作为应用程序和数据库 ...

  7. 比较C++中的4种类型转换方式(转自http://blog.csdn.net/hrbeuwhw/article/details/7884797)

    C++的四种cast操作符的区别 Q:什么是C风格转换?什么是static_cast, dynamic_cast 以及 reinterpret_cast?区别是什么?为什么要注意? A:转换的含义是通 ...

  8. CodeForces 698B Fix a Tree

    并查集,构造. 先看一下图的特殊性,按照这种输入方式,一个点的入度最多只有$1$,因此,问题不会特别复杂,画画图就能知道了. 如果给出的序列中已经存在$a[i]=i$,那么随便取一个$a[i]=i$的 ...

  9. 第九十一节,html5+css3pc端固定布局,完成首页

    html5+css3pc端固定布局,完成首页 此时我们的首页就完成了 首页效果 其他页面我就不做了,原理相同,做其他页面时将头尾css分离调用即可 大纲算法 我们看看大纲算法比较清晰,说明符合规则 h ...

  10. ubuntu 禁用快捷键

    System Settings > Keyboard > Shortcuts 点击要禁用的快捷键,按键盘"Backspace"键,就可以禁用了