# 文章标题

Given a sequence of K integers { N​1​​ , N​2, …, N​K​​ }. A continuous subsequence is defined to be { N​i​​ , N​i+1​​ , …, N​j​​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21


Sample Output:

10 1 4


#include <stdio.h>
int main(){
int a[10000],i=0,top=0,sum=0,h,r,t,z=1,l,x=1;
int k,k1;
scanf("%d",&k);
k1=k;
while(k1){
scanf("%d",&a[i]);
i++;
k1--;
}
h=r=t=0;
l=a[0];
for(i=0;i<k;i++){
if(a[i]>0) {
z=0;
}
//  else if(i>0){
//      if(a[i]>l){
//          l=a[i];
//      }
//  }
if(a[i]==0) x=0;
sum+=a[i];
if(sum<0){
sum=0;
t=i+1;
}

if(sum>top) {
top=sum;
r=i;
if(t!=h){
h=t;
}
}
}
if(z){
switch(x){
case 1:
top=0;
h=0;
r=k-1;
break;
case 0:
top=0;
a[h]=0;
a[r]=0;
}
}
printf("%d %d %d",top,a[h],a[r]);
return 0;
} 

    if(a[i]>0) {
z=0;
}else if(i>0){
if(a[i]>l){
l=a[i];
}

x是再后来加的，用于0和负数混合输入的情况

• 本文已收录于以下专栏：

## 【Java】获取指定HTML 文档指定的body、页中超链接的标题和链接、指定博客文章的内容

• 2017年08月20日 22:09
• 2.59MB
• 下载

## 文章标题自动标注程序

• 2015年06月25日 17:04
• 6.93MB
• 下载

## 文章标题上下轮番滚动.zip

• 2012年09月04日 12:31
• 28KB
• 下载

## 文章标题 UVALive 7045：Last Defence（辗转相除思想）

Last DefenceGiven two integers A and B. Sequence S is deﬁned as follow: • S0 = A • S1 = B • Si = |Si...

## 文章标题：Android常见控件ImageView的使用方法

ImageView是用于在界面上展示图片的一个控件，通过它可以上我们的程序界面变得更加丰富多彩。我们需要在drawable中放一张格式为Png的图片。那我们在界面上展示这张图片吧。我们在activit...
• wen0801
• 2017年04月06日 00:16
• 103