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的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  2. 【81.37%】【codeforces 734B】Anton and Digits

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  3. 【codeforces 602D】Lipshitz Sequence

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  5. 【搜索】【并查集】Codeforces 691D Swaps in Permutation

    题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...

  6. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  7. 【链表】【模拟】Codeforces 706E Working routine

    题目链接: http://codeforces.com/problemset/problem/706/E 题目大意: 给一个N*M的矩阵,Q个操作,每次把两个同样大小的子矩阵交换,子矩阵左上角坐标分别 ...

  8. 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions

    题目链接: http://codeforces.com/problemset/problem/710/D 题目大意: 两个等差数列a1x+b1和a2x+b2,求L到R区间内重叠的点有几个. 0 < ...

  9. 【动态规划】【最短路】Codeforces 710E Generate a String

    题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...

随机推荐

  1. 【记录】无法读取配置节“AppSettings”,因为它缺少节声明

    Web.config对大小写敏感, 把AppSettings改为appSettings即可.

  2. 03007_JDBC概述

    1.JDBC概述 (1)JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用J ...

  3. checkbox-padding 调整checkbox字体跟图标距离

    有时候我们会遇到需要调整控件中的内容相对于容器的位置.这里有两种情况 1.linearlayout这样的容器中,包含button类的控件,这时候margin可以调节 2.textview中的文字内容 ...

  4. thinkphp5 left join

    thinkphp5 left join 一.总结 1.作用:left join就是即使不匹配也返回左表中的数据 2.join使用通式:object join ( mixed join [, mixed ...

  5. 41.C++多线程生产消费者模型

    #include <iostream> #include <thread> #include <mutex> #include <condition_vari ...

  6. 28.lambda表达式与多线程

    #include <iostream> #include <thread> #include <Windows.h> #include <chrono> ...

  7. java匿名内部类使用场景列举

    示例一: package com;      interface Operation {       double operateTwoIntNum(int a, int b);   }      p ...

  8. LiveReload配置,安装使用方法~~~前端页面神助手

    一.Chrome端安装LiveReload插件 1.首先这里啰嗦一下,如果Chrome无法进入商店,可以先安装一下谷歌商店助手 谷歌商店插件地址 http://pan.baidu.com/s/1dF1 ...

  9. bootstrap课程8 bootstrap导航条在不同设备上的显示效果如何

    bootstrap课程8 bootstrap导航条在不同设备上的显示效果如何(多去看参考手册) 一.总结 一句话总结:在手机端或者平板端或者显示不够的话就缩起来了.(多去看参考手册) 二.bootst ...

  10. js无缝滚动原理及详解[转自刹那芳华]

    刚刚接触JS,网上找了一些关于无缝滚动的教程,但都大同小异,对我这种新手来说也只是会用,不知道什么意思,想要自己写个更是一头雾水.于是找了一些资料,详细说明一下JS无缝滚动的原理,相信看过这篇文章之后 ...