Josif_tepe

Untitled

Nov 9th, 2025
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. typedef long long ll;
  5. const int maxn = 50005;
  6. struct node_data {
  7.     int sum, prefix, suffix, res;
  8. };
  9.  
  10. node_data merge_nodes(node_data A, node_data B) {
  11.     node_data C;
  12.    
  13.     C.sum = A.sum + B.sum;
  14.     C.prefix = max(A.prefix, A.sum + B.prefix);
  15.     C.suffix = max(B.suffix, B.sum + A.suffix);
  16.    
  17.     C.res = max(max(A.res, B.res), A.suffix + B.prefix);
  18.    
  19.     return C;
  20. }
  21.  
  22. node_data make_node(int value) {
  23.     node_data C;
  24.     C.sum = value;
  25.     C.prefix = max(0, value);
  26.     C.suffix = max(0, value);
  27.     C.res = max(0, value);
  28.    
  29.     return C;
  30. }
  31. int main() {
  32.     ios_base::sync_with_stdio(false);
  33.    
  34.     node_data A = make_node(2);
  35.     node_data B = make_node(-3);
  36.     node_data C = make_node(4);
  37.    
  38.     A = merge_nodes(A, B);
  39.     C = merge_nodes(A, C);
  40.    
  41.     cout << C.res << endl;
  42.     return 0;
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment