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
  1. 5 4
    1 -2 1 3 -4
    1 2
    4 5
    3 4
    1 4
output
  1. 7
input
  1. 4 3
    1 2 3 4
    1 3
    2 4
    1 1
output
  1. 16
input
  1. 2 2
    -1 -2
    1 1
    1 2
output
  1. 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,则对答案有贡献;

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <cstring>
  8. #include <string>
  9. #include <set>
  10. #include <map>
  11. #include <unordered_map>
  12. #include <queue>
  13. #include <stack>
  14. #include <vector>
  15. #include <list>
  16. #define rep(i,m,n) for(i=m;i<=n;i++)
  17. #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
  18. #define mod 1000000007
  19. #define inf 0x3f3f3f3f
  20. #define vi vector<int>
  21. #define pb push_back
  22. #define mp make_pair
  23. #define fi first
  24. #define se second
  25. #define ll long long
  26. #define pi acos(-1.0)
  27. #define pii pair<int,int>
  28. #define Lson L, mid, ls[rt]
  29. #define Rson mid+1, R, rs[rt]
  30. #define sys system("pause")
  31. #define intxt freopen("in.txt","r",stdin)
  32. const int maxn=1e5+;
  33. using namespace std;
  34. int gcd(int p,int q){return q==?p:gcd(q,p%q);}
  35. ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
  36. inline ll read()
  37. {
  38. ll x=;int f=;char ch=getchar();
  39. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  40. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  41. return x*f;
  42. }
  43. int n,m,k,t,a[maxn],sum[maxn];
  44. int main()
  45. {
  46. int i,j;
  47. scanf("%d%d",&n,&m);
  48. rep(i,,n)scanf("%d",&a[i]),sum[i]=sum[i-]+a[i];
  49. ll ans=;
  50. rep(i,,m)
  51. {
  52. int l,r;
  53. scanf("%d%d",&l,&r);
  54. if(sum[r]-sum[l-]>)ans+=sum[r]-sum[l-];
  55. }
  56. printf("%lld\n",ans);
  57. //system("Pause");
  58. return ;
  59. }

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. vue实例生命周期

    实例生命周期 var vm = new Vue({ data: { a: 1 }, created: function () { // `this` 指向 vm 实例 console.log('a i ...

  2. 关于在mfc中cstring转为float和ini

    CString str1,str, str2; GetDlgItemText(IDC_EDIT1, str1); GetDlgItemText(IDC_EDIT2, str2); UINT value ...

  3. 反射机制(reflection)动态相关机制

    功能:动态获取类的信息以及动态调用对象的方法. Java反射机制主要提供了以下功能: 1.在运行时判断任意一个对象所属的类. 2.在运行时构造任意一个类的对象. 3.在运行时判断任意一个类所具有的成员 ...

  4. JavaFX基础学习之URLConnection

    一个标准的JavaFX文件包含三个部分:主类 . 控制类. 界面设计(XML+CSS) 1,main.java package application; import javafx.applicati ...

  5. yii2.0单文件上传和多文件上传

    yii2文件上传使用到yii2自带的文件上传类UploadFIle,以及对应的模型规则,这里分别介绍单文件上传和多文件上传: yii2单个文件上传: 上传步奏,先创建上传表单模型model(包含验证规 ...

  6. linux提示语言包

    locale -a 查看可用的语言包.选择中文语言包 echo 'LC_ALL="zh_CN.utf8"' >> /etc/profileecho 'export LC ...

  7. Toast用法

    应用场景:弹出提示信息 主界面: 代码如下: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(sa ...

  8. modelsim+win环境下systemverilog调用c函数

    最近为了验证一个ip,需要将ip的输出数据与c model的数据比对,之前采用的是将仿真结果输出,用perl读取结果,与c的输出结果比对,这样做也可以,但是在做遍历测试时,由于数据量较大,就显得不方便 ...

  9. oracle 使用技巧

    1.PL/SQL Developer记住登陆密码  在使用PL/SQL Developer时,为了工作方便希望PL/SQL Developer记住登录Oracle的用户名和密码: 设置方法:PL/SQ ...

  10. Centos6.3手动rpm安装gcc,c++

    如果你的服务器是不能上网的,那就说明你要手动安装很多软件,比如gcc; 1,首先到http://vault.centos.org/6.3/os/x86_64/Packages/下载用到的rpm包,包括 ...