#include <bits/stdc++.h>
using namespace std;
struct TreeNode{
	int val;
	TreeNode*left;
	TreeNode*right;
	TreeNode(int val):left(nullptr),right(nullptr),val(val){};
};

int path(TreeNode*root,int&maxi){
	if(root == nullptr)return 0;
	int left = max(0,path(root->left,maxi));
	int right = max(0,path(root->right,maxi));
	
	maxi = max(maxi,left+right+root->val);
	return max(left,right)+root->val;
}
int sum(TreeNode* root){
	int maxi = INT_MIN;
	path(root,maxi);
	return maxi;
}
TreeNode* buildTree(){
	int x;cin>>x;
	if(x== -1)return nullptr;
	TreeNode* root = new TreeNode(x);
	
	queue<TreeNode*>q;
	q.push(root);
	
	while(!q.empty()){
		auto u= q.front();q.pop();
		if(cin>>x&&x!=-1){
			u->left= new TreeNode(x);
			q.push(u->left);
		}
		
			if(cin>>x&&x!=-1){
			u->right= new TreeNode(x);
			q.push(u->right);
		}
	}
	return root;
}
int main() {
    TreeNode* root = buildTree();
    cout<<sum(root);
	return 0;
}