Submission #237738

# Submission time Handle Problem Language Result Execution time Memory
237738 2020-06-08T14:25:34 Z Haunted_Cpp Feast (NOI19_feast) C++17
22 / 100
74 ms 15980 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 3e5 + 5;

vector<long long> arr (N);
vector<long long> comp;
vector< vector<long long> > g (N);
bool is_rem [N];

template<typename T>
struct Kadane {
  long long best;
  long long cur;
  int s, start, end;
  Kadane (vector<T> &a) {
    best = cur = s = start = end = 0;
    for (int i = 0; i < (int) a.size(); i++) {
      cur += a[i];
      if (cur < 0) {
        cur = 0;
        s = i + 1;
      }
      if (cur > best) {
        best = cur;
        start = s;
        end = i + 1;
      }
    }
  }
};

int main () {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < n; i++) {
    if (comp.empty() || ( (arr[i] >= 0)  ^ (comp.back() >= 0) ) == true ) {
      comp.emplace_back(arr[i]);
      continue;
    }
    long long cur = arr[i];
    while (!comp.empty() && ( (arr[i] >= 0)  ^ (comp.back() >= 0) ) == false ) {
      cur += comp.back();
      comp.pop_back();
    }
    comp.emplace_back(cur);
  }
  int tot = count_if (comp.begin(), comp.end(), [&] (int x) {
    return (x > 0);
  });
  if (tot <= k) {
    long long s = 0;
    for (auto to : comp) s += (to >= 0 ? to : 0);
    cout << s << '\n';
    return 0;
  }
  int primeiro = -1;
  int ultimo = -1;
  for (int i = 0; i < (int) comp.size(); i++) {
    if (comp[i] > 0) {
      if (primeiro == -1) primeiro = i;
      else ultimo = i + 1;
    }
  }
  vector<long long> neg;
  for (int i = primeiro; i < ultimo; i++) {
    if (comp[i] < 0) {
      neg.emplace_back(i);
    }
  }
  sort (neg.begin(), neg.end(), [&] (int x, int y) {
    return comp[x] < comp[y];
  });
  int blocos = 1;
  for (auto to : neg) {
    if (blocos < k) {
      ++blocos;
      is_rem[to] = true;
    }
  }
  int idx = 0;
  for (int i = primeiro; i < ultimo; i++) {
    if (is_rem[i]) {
      ++idx;
      continue;
    }
    g[idx].emplace_back(comp[i]);
  }
  long long res = 0;
  for (auto vec : g) {
    if (!vec.empty()) {
      Kadane <long long> solve (vec); 
      res += solve.best;
    }
  }
  cout << res << '\n';
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 49 ms 9728 KB Output is correct
2 Correct 53 ms 9728 KB Output is correct
3 Correct 53 ms 9728 KB Output is correct
4 Correct 51 ms 9728 KB Output is correct
5 Correct 51 ms 9728 KB Output is correct
6 Correct 49 ms 9728 KB Output is correct
7 Correct 49 ms 9728 KB Output is correct
8 Correct 51 ms 9728 KB Output is correct
9 Correct 50 ms 9728 KB Output is correct
10 Correct 50 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 9728 KB Output is correct
2 Correct 31 ms 9728 KB Output is correct
3 Correct 32 ms 9728 KB Output is correct
4 Correct 32 ms 9728 KB Output is correct
5 Incorrect 49 ms 9728 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 71 ms 14828 KB Output is correct
2 Correct 68 ms 14828 KB Output is correct
3 Correct 70 ms 14828 KB Output is correct
4 Correct 68 ms 14700 KB Output is correct
5 Correct 71 ms 14768 KB Output is correct
6 Correct 71 ms 15928 KB Output is correct
7 Correct 68 ms 15852 KB Output is correct
8 Correct 71 ms 14828 KB Output is correct
9 Correct 72 ms 15852 KB Output is correct
10 Correct 74 ms 15980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Incorrect 12 ms 9728 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Incorrect 12 ms 9728 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Incorrect 12 ms 9728 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 49 ms 9728 KB Output is correct
2 Correct 53 ms 9728 KB Output is correct
3 Correct 53 ms 9728 KB Output is correct
4 Correct 51 ms 9728 KB Output is correct
5 Correct 51 ms 9728 KB Output is correct
6 Correct 49 ms 9728 KB Output is correct
7 Correct 49 ms 9728 KB Output is correct
8 Correct 51 ms 9728 KB Output is correct
9 Correct 50 ms 9728 KB Output is correct
10 Correct 50 ms 9728 KB Output is correct
11 Correct 31 ms 9728 KB Output is correct
12 Correct 31 ms 9728 KB Output is correct
13 Correct 32 ms 9728 KB Output is correct
14 Correct 32 ms 9728 KB Output is correct
15 Incorrect 49 ms 9728 KB Output isn't correct
16 Halted 0 ms 0 KB -