int maxSum(TreeNode *root, int *maxChild) { int lm = 0; int rm = 0; *maxChild = 0; if (!root) return 0; int left = maxSum(root->left, &lm); int right = maxSum(root->right, &rm); int a = max(lm, rm); *maxChild = root->val; if (a > 0) *maxChild += a; a = 0; if (lm > 0) a += lm; if (rm > 0) a += rm; a += root->val; if (root->left) a = max(a, left); if (root->right) a = max(a, right); return a; }
int maxSum(struct node* root, int* max) { if(root == NULL) return 0; int sum = maxSum(root->left, max) + maxSum(root->right, max) + root->data; if(sum > *max) *max = sum; return sum; }
int maxSum(TreeNode* root) { if (root == NULL) return 0; int leftSum = maxSum(root->left); int rightSum = maxSum(root->right); if (leftSum + rightSum + root->val > r) { r = leftSum + rightSum + root->val; } return max(0, max(leftSum, rightSum) + root->val); }
void pull( Treap * a ){ a->sum = Sum( a->l ) + Sum( a->r ) + a->val; a->lsum = Sum( a->l ) + a->val + max( 0 , lSum( a->r ) ); if( a->l ) a->lsum = max( lSum( a->l ) , a->lsum ); a->rsum = Sum( a->r ) + a->val + max( 0 , rSum( a->l ) ); if( a->r ) a->rsum = max( rSum( a->r ) , a->rsum ); a->maxsum = max( 0 , rSum( a->l ) ) + a->val + max( 0 , lSum( a->r ) ); a->maxsum = max( a->maxsum , max( maxSum( a->l ) , maxSum( a->r ) ) ); a->sz = Size( a->l ) + Size( a->r ) + 1; }
int maxSum(Node *root){ if(!root) return 0; int leftSum = maxSum(root->left); int rightSum = maxSum(root->right); int max =( (leftSum > rightSum) ? leftSum:rightSum ); return (max + root->data); }
//函数返回当前节点为根的最大和 int maxSum(TreeNode* root,int &sum){ if(root==NULL) return INT_MIN; sum=root->val>sum?root->val:sum; int leftSum=maxSum(root->left,sum); int rightSum=maxSum(root->right,sum); leftSum=leftSum>0?leftSum:0; //若左子树和为负数,就直接设为0 rightSum=rightSum>0?rightSum:0; //和左子树相同 sum=max(leftSum+root->val+rightSum,sum); return root->val+max(leftSum,rightSum); }
int maxSum(TreeNode* root) { if(!root) return 0; if(!root->left && !root->right) { max_global = std::max(max_global,root->val); return root->val; } int left_val = maxSum(root->left); int right_val = maxSum(root->right); int max_local = std::max(root->val, std::max(left_val+root->val, right_val+root->val)); max_global = std::max(max_local, std::max(left_val+right_val+root->val, max_global)); return max_local; }
int maxSum(TreeNode* root,int& mymax){ if(!root) return 0; int left=maxSum(root->left,mymax); int right=maxSum(root->right,mymax); int localmax=root->val; if(left>0) localmax+=left; if(right>0) localmax+=right; mymax=max(mymax,localmax); return max(root->val,max(root->val+left,root->val+right)); }
int maxSum(TreeNode *root) { if (!root) { return 0; } int l = maxSum(root->left), r = maxSum(root->right); int cur_max = (l > 0 ? l : 0) + (r > 0 ? r : 0) + root->val; if (cur_max > ans) { ans = cur_max; } return max(max(l, r) + root->val, 0); }
// Value may be negative!! int maxPathSum(TreeNode* root) { // Start typing your C/C++ solution below // DO NOT write int main() function r = INT_MIN; maxSum(root); return r; }
int main() { int array[10] = {1, -2, 3, 4, -5, 6, -7, 8, 9, 10}; int max = maxSum(array, sizeof(array)/sizeof(int)); printf("max sum of array is : %d\n", max); return 0; }
int maxPathSum(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function int mymax=root->val; int val=maxSum(root,mymax); return max(mymax,val); }
int main() { int a[10]={1, -2, 3, 10, -4, 7, 2, -5}; //int a[]={-1,-2,-3,-4}; //测试全是负数的用例 printf("子数组的最大和为:%d\n",maxSum(a,8)); return 0; }
int main() { int i,N,T; int tmp = 0; scanf("%d",&T); while(T > 0) { scanf("%d",&N); for(i = 0;i < N;i++) { scanf("%d",&a[i]); } T--; // for(i = 0;i < N;i++) // { // printf("%d ",a[i]); // } // printf("\n"); printf("Case %d:\n",++tmp); printf("%d",maxSum(a,N)); printf(" %d %d\n",first,last); if(T > 0) printf("\n"); } return 0; }
// kadane's algo! int main() { int a[] = {-2,1,-3,4,-1,2,1,-5,4}; int i = maxSum(a, 9); printf("%d\n", i); return 0; }
int maxPathSum(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function //if (!root) return 0; int c = 0; int r = maxSum(root, &c); return r; }
int main() { int a[]={1,5,9,8,2,2,4,1},maxsum; maxsum=maxSum(a,sizeof(a)/sizeof(a[0])); printf("max sum is: %d",maxsum); getch(); }
void solve(){ Treap *t = NULL , *tl = NULL , *tr = NULL; n = getint(); m = getint(); for( int i = 0 ; i < n ; i ++ ) t = merge( t , new Treap( getint() ) ); while( m -- ){ scanf( "%s" , c ); if( c[ 0 ] == 'I' ){ int p , k; p = getint(); k = getint(); split( t , p , tl , tr ); t = NULL; while( k -- ) t = merge( t , new Treap( getint() ) ); t = merge( t , tr ); t = merge( tl , t ); }else if( c[ 0 ] == 'D' ){ int p , k; p = getint(); k = getint(); split( t , p - 1 , tl , t ); split( t , k , t , tr ); Delete( t ); t = merge( tl , tr ); }else if( c[ 0 ] == 'R' ){ int p , k; p = getint(); k = getint(); split( t , p - 1 , tl , t ); split( t , k , t , tr ); t->tag ^= 1; int swp = t->lsum; t->lsum = t->rsum; t->rsum = swp; t = merge( t , tr ); t = merge( tl , t ); }else if( c[ 0 ] == 'G' ){ int p , k; p = getint(); k = getint(); split( t , p - 1 , tl , t ); split( t , k , t , tr ); printf( "%d\n" , Sum( t ) ); t = merge( t , tr ); t = merge( tl , t ); }else if( c[ 2 ] == 'K' ){ int p , k; p = getint(); k = getint(); split( t , p - 1 , tl , t ); split( t , k , t , tr ); t->tagn = true; t->num = getint(); t->sum = t->num * t->sz; if( t->num >= 0 ) t->lsum = t->rsum = t->maxsum = t->sum; else t->lsum = t->rsum = t->maxsum = t->num; t = merge( t , tr ); t = merge( tl , t ); }else printf( "%d\n" , maxSum( t ) ); } }
int maxPathSum(TreeNode *root) { if (!root) { return 0; } maxSum(root); return ans; }
int maxSum(TreeNode * head) { //返回的时候只能返回一条边给上层 //但是计算当前node的时候,需要把两条边算进去。 int ret = INT_MIN; int left = 0, right = 0; if (head->left) left = maxSum(head->left); if (head->right) right = maxSum(head->right); int curMax = (left > 0 ? left : 0) + (right > 0 ? right : 0) + head->val; int m = left > right?left:right; if(m > 0 ) ret = m + head->val; else ret = head->val; sum = max(curMax, sum); return ret; }
int maxSum(int left, int right) { int mid, sum, maxToLeft, maxToRight, maxCrossing, maxInA, maxInB; int i; // Zero element Vector if (left>right) { return 0; } // One element Vector if (left==right) { return max(0,list[left]); } // A is List[Left...Mid], B is List[Mid+1...Right] mid = (left + right)/2; // Find max crossing to left sum=0; maxToLeft=0; for(i=mid; i>=left; i--) { sum = sum + list[i]; maxToLeft = max(maxToLeft,sum); } // Find max crossing to right sum=0; maxToRight=0; for(i=mid+1; i<=right; i++) { sum = sum + list[i]; maxToRight = max(maxToRight,sum); } maxCrossing = maxToLeft + maxToRight; maxInA = maxSum(left,mid); maxInB = maxSum(mid+1,right); return max(max(maxCrossing,maxInA),maxInB); }
int main(){ struct node *root = NULL; /* Constructing tree given in the above figure */ root = newNode(10); root->left = newNode(-2); root->right = newNode(7); root->left->left = newNode(8); root->left->right = newNode(-4); std::cout<<"max root to leaf sum:"<<maxSum(root)<<std::endl; return 0; }
int main() { scanf("%d",&t); for(i=1; i<=t; i++) { init(); scanf("%d",&n); for(j=0; j<n; j++) { scanf("%d",&a[j]); } maxSum(a); printf("Case %d:\n",i); printf("%d %d %d\n",max,lflag,rflag); if(i!=t)printf("\n"); } return 0; }
int main() { struct node *root = newNode(1); root->left = newNode(-10); root->right = newNode(-30); root->left->left = newNode(4); root->left->right = newNode(5); root->left->left->left = newNode(-6); int max=0; maxSum(root, &max); printf("maxSum = %d\n", max); return 0; }
int main() { int a[SIZE]; int n = sizeof(a) / sizeof(a[0]); // 배열 원소의 갯수 clock_t t1, t2, t3, t4; init(a, n); t1 = clock(); printf("maxSum : %d\n", maxSum(a, n)); t2 = clock(); printf("fastMaxSum : %d\n", fastMaxSum(a, n)); t3= clock(); printf("fastestMaxSum : %d\n", fastestMaxSum(a, n)); t4 = clock(); // 수행시간 측정 printf("maxSum : %lf\n", (double)(t2 - t1) / CLOCKS_PER_SEC); printf("fastMaxSum : %lf\n", (double)(t3 - t2) / CLOCKS_PER_SEC); printf("fastestMaxSum : %lf\n", (double)(t4 - t3) / CLOCKS_PER_SEC); return 0; }
int main(int argc, char** argv) { printf ("%d \n", maxSum(0,4)); return 0; }
int maxPathSum(TreeNode* root) { int sum=INT_MIN; //保存最大值 maxSum(root,sum); return sum; }
int maxPathSum(TreeNode *root) { sum = root->val; maxSum(root); return sum; }
/////implement APcluser algorithm void APcluster(float s[][R],float p[],int idx[]) { float Max(float a[][R],float s[][R],int i); float maxSum(float z,float r[][R],int k); float lam=L;//¦Ë int i,j,k,k1; int times=0,mx=0; float a[R][R],r[R][R],e[R][R]; float sum=0.0,tem=0.0,old=0.0,Rdis=0.0,Rdsum=0.0,Rdaver=0.0,Adis=0.0,Adsum=0.0,Adaver=0.0; bool flag=true; //////Initialize/// for(i=0;i<R;i++) { for(j=0;j<R;j++) { a[i][j]=0.0; r[i][j]=0.0; e[i][j]=0.0; } } /////iterations//// while(flag) { times++; Rdsum=0.0; Adsum=0.0; ///Responsibility for(i=0;i<R;i++) { k=0; for(k=0;k<R;k++) { old=r[i][k]; r[i][k]=s[i][k]-Max(a,s,i); //formula1:r(i,k)<-s(i,k)-max{a(i,k')+s(i,k')} r[i][k]=(1-lam)*old+lam*r[i][k]; //formula2:r(new)=¦Ë*r(new)+(1-¦Ë)*r(old) Rdis=r[i][k]-old; //calculate the change // printf("Rdis(%d,%d):%f ",i,k,Rdis); Rdsum=Rdsum+Rdis; //sum of the changes } } Rdaver=Rdsum/R; //calculate the average of the sum of the changes //printf("\nRdaver %f , ",Rdaver); //test ///availabilities for(j=0;j<R;j++) { k1=0; while(k1<R) { old=a[j][k1]; if(j==k1) { a[j][k1]=maxSum(0.0,r,k1); } else { tem=r[k1][k1]+maxSum(0.0,r,k1); //formula3: r(k,k)+sum of max{0,r(i',k)} a[j][k1]=(0.0<tem)?0.0:tem; //formula4:min{0,formula3} } a[j][k1]=(1-lam)*old+lam*a[j][k1]; //formula:r(new)=¦Ë*r(new)+(1-¦Ë)*r(old) Adis=a[j][k1]-old; //calculate the change Adsum=Adsum+Adis; //sum of the changes k1++; } //while } //for Adaver=Adsum/R; //calculate the average of the sum of the changes for(i=0;i<R;i++) { for(j=0;j<R;j++) { e[i][j]=a[i][j]+r[i][j]; } } //e(i,k)=maxk{e(i,k)},then make the exemplar decisions for(i=0;i<R;i++) { mx=0; for(j=0;j<R;j++) { if(e[i][j]>e[i][mx]) { mx=j; } } idx[i]=mx; } //printf("\nAdaver %f , ",Adaver); //test if(((Rdaver*Rdaver)<Threshold) && ((Adaver*Adaver) <Threshold)||(times>MAXI)) flag=false; } printf("Iterators : %d\n ",times); }
int maxPathSum(TreeNode* root) { max_global = root->val; maxSum(root); return max_global; }