Just Eat It!

Today, Yasser and Adel are at the shop buying cupcakes. There are nn cupcake types, arranged from 11 to nn on the shelf, and there are infinitely many of each type. The tastiness of a cupcake of type ii is an integer aiai. There are both tasty and nasty cupcakes, so the tastiness can be positive, zero or negative.

Yasser, of course, wants to try them all, so he will buy exactly one cupcake of each type.

On the other hand, Adel will choose some segment [l,r][l,r] (1≤l≤r≤n)(1≤l≤r≤n) that does not include all of cupcakes (he can’t choose [l,r]=[1,n][l,r]=[1,n]) and buy exactly one cupcake of each of types l,l+1,…,rl,l+1,…,r.

After that they will compare the total tastiness of the cupcakes each of them have bought. Yasser will be happy if the total tastiness of cupcakes he buys is strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel’s choice.

For example, let the tastinesses of the cupcakes be [7,4,−1][7,4,−1]. Yasser will buy all of them, the total tastiness will be 7+4−1=107+4−1=10. Adel can choose segments [7],[4],[−1],[7,4][7],[4],[−1],[7,4] or [4,−1][4,−1], their total tastinesses are 7,4,−1,117,4,−1,11 and 33, respectively. Adel can choose segment with tastiness 1111, and as 1010 is not strictly greater than1111, Yasser won’t be happy 😦

Find out if Yasser will be happy after visiting the shop.

Input
Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). The description of the test cases follows.The first line of each test case contains nn (2≤n≤1052≤n≤105).The second line of each test case contains nnintegers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109), where aiai represents the tastiness of the ii-th type of cupcake.It is guaranteed that the sum of nn over all test cases doesn’t exceed 105105.

Output
For each test case, print “YES”, if the total tastiness of cupcakes Yasser buys will always bestrictly greater than the total tastiness of cupcakes Adel buys regardless of Adel’s choice. Otherwise, print “NO”.

ExampleInput
3
4
1 2 3 4
3
7 4 -1
3
5 -5 5

Output
YES
NO
NO
Note

题意
n个糖果,两个人,A拿全部,B拿部分,问A拿的能否比B多

思路
维护最大子段,从1-n-1扫一遍,再从2-n扫一遍

代码

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll INF=0x3f3f3f3f;
ll a[100005],sum,ans,k;
int t,n;
int main()
{
 scanf("%d",&t);
 while(t--)
 {
 scanf("%d",&n);
  ans=-INF;
  sum=0;
  for(int i=1;i<=n;i++)
  {
   scanf("%lld",&a[i]);
   sum+=a[i];
  }
  k=0;
  for(int i=1;i<n;i++)
  {
   k+=a[i];
   ans=max(ans,k);
   if(k<0)
    k=0;
  }
  k=0;
  for(int i=2;i<=n;i++)
  {
   k+=a[i];
   ans=max(ans,k);
   if(k<0)
    k=0;
  }
  if(sum>ans)
   printf("YES\n");
  else
   printf("NO\n");
 }
 return 0;
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值