1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;

#include "message.h"
#include "maklib.h"

namespace {
  struct Result {
    long long total;
    long long best;
    long long best_pref;
    long long best_suf;
  };

  void send(int target, Result const& res)
  {
    PutLL(target, res.total);
    PutLL(target, res.best);
    PutLL(target, res.best_pref);
    PutLL(target, res.best_suf);
    Send(target);
  }

  pair<int, Result> receive(int source=-1)
  {
    source = Receive(source);
    Result res;
    res.total = GetLL(source);
    res.best = GetLL(source);
    res.best_pref = GetLL(source);
    res.best_suf = GetLL(source);
    return {source, move(res)};
  }

  Result calculate(std::vector<int> const& data)
  {
    Result res;
    res.total = accumulate(data.begin(), data.end(), 0);
    res.best = 0;
    res.best_pref = 0;
    res.best_suf = 0;
    long long cur = 0;
    long long cur_pref = 0;
    long long cur_suf = 0;
    int n = data.size();
    for (int i = 0; i < n; ++i) {
      int x = data[i];
      int y = data[n-1-i];
      cur = max(0LL, cur + x);
      cur_pref += x;
      cur_suf += y;
      res.best = max(res.best, cur);
      res.best_pref = max(res.best_pref, cur_pref);
      res.best_suf = max(res.best_suf, cur_suf);
    }
    return res;
  }

  long long combine(vector<Result> const& results)
  {
    long long cur = 0;
    long long best = 0;
    for (auto const& r: results) {
      best = max(best, r.best);
      best = max(best, cur + r.best_pref);
      cur = max(r.best_suf, cur + r.total);
    }
    return best;
  }
}

int main()
{
  int n = Size();
  int k = min(NumberOfNodes(), n);
  int id = MyNodeId();
  if (id >= k) return 0;

  int l = (id + 0LL) * n / k;
  int r = (id + 1LL) * n / k;
  vector<int> data;
  data.reserve(r-l);
  for (int i = l; i < r; ++i) {
    data.push_back(ElementAt(i+1));
  }
  Result result = calculate(move(data));
  if (id > 0) {
    send(0, move(result));
  } else {
    vector<Result> results;
    results.reserve(k);
    results.push_back(move(result));
    for (int i = 1; i < k; ++i) {
      results.push_back(move(receive(i).second));
    }
    cout << combine(move(results)) << '\n';
  }

  return 0;
}