Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- typedef long long ll;
- const int maxn = 50005;
- struct node_data {
- int sum, prefix, suffix, res;
- };
- node_data merge_nodes(node_data A, node_data B) {
- node_data C;
- C.sum = A.sum + B.sum;
- C.prefix = max(A.prefix, A.sum + B.prefix);
- C.suffix = max(B.suffix, B.sum + A.suffix);
- C.res = max(max(A.res, B.res), A.suffix + B.prefix);
- return C;
- }
- node_data make_node(int value) {
- node_data C;
- C.sum = value;
- C.prefix = max(0, value);
- C.suffix = max(0, value);
- C.res = max(0, value);
- return C;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- node_data A = make_node(2);
- node_data B = make_node(-3);
- node_data C = make_node(4);
- A = merge_nodes(A, B);
- C = merge_nodes(A, C);
- cout << C.res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment