Popping Balloons (Problem F, 2018 ICPC Asia Jakarta Regional Contest, 2018)
Time limit: 1 sec
Memory limit: 256MB
Author: Alham Fikri Aji

# Problem Statement

Ayu is participating in ABC 2018 (Arranging Blocks Competition). In this competition, each contestant is given $M$ minutes and $N$ tasks which should be solved one-by-one in the given order. The contestant who solves the most tasks is the winner. What makes this contest interesting to you (ICPC contestants) is that this contest uses a similar encouragement as ICPC, i.e. balloons. In particular, each time a contestant solves a task, s/he will be given a balloon.

Ayu is convinced that she can defeat all other contestants, except one particular contestant, Budi, her rival. Ayu knows well her skill, i.e. she knows exactly how long it takes for her to solve a particular task. Unsurprisingly, Ayu also knows well Budi’s skill (they are rival for a reason). Let there be two arrays of integers $A_{1..N}$ and $B_{1..N}$. $A_i$ denotes the time (in minutes) needed by Ayu to solve the $i^{th}$ task, while $B_i$ denotes the time (in minutes) needed by Budi to solve the same $i^{th}$ task.

Here comes the interesting part. Ayu knows that Budi is very sensitive to any disturbingly loud sound like a balloon being popped. Whenever Budi is surprised (due to a balloon being popped), he will lose his concentration and has to repeat the task he’s doing. For example, suppose Budi needs 10 minutes to solve a particular task. If a balloon pops at the $7^{th}$ minute, then Budi repeats the task at the $7^{th}$ minute (out of his 10 minutes), causing him to solve the task with $7 + 10 = 17$ minutes. If two balloon pops, each at the $7^{th}$ and $13^{th}$ minute, then Budi repeats the task at minute $7^{th}$ (out of his 10 minutes), repeats it again at minute $6^{th}$ (out of his 10 minutes), and finally solved the task with $7 + 6 + 10 = 23$ minutes. If a balloon pops at the same time Budi supposed to solve the task (i.e. at the $10^{th}$ minute in this example), then Budi will also not solve that task. Therefore, Budi has to spend another 10 minutes (for a total of $10 + 10 = 20$ minutes) to solve that particular task in this case.

Ayu plans to exploit Budi’s weakness in order to defeat him, i.e. Ayu will strategically use the balloons (popping them at integer minutes) she gets from solving the tasks. Your task in this problem is to find out whether it is possible for Ayu to have the number of solved tasks to be strictly larger than Budi’s. If it is possible, you should output one (any) working plan on when she should pop the balloons.

Note that if Ayu solves a task at exactly the $M^{th}$ minute, then the task is considered as solved. Similarly, if Budi solves a task at exactly the $M^{th}$ minute, then the task is considered as solved, except if Ayu decides to pop a balloon at the same time. Also, Ayu can pop a balloon as soon as she receives it. Ayu cannot pop more than one balloon at the same minute. She also cannot pop any balloon after the $M^{th}$ minute mark.

# Input

Input begins with a line containing two integers: $N$ $M$ ($1 \le N \le 100000$, $1 \le M \le 10^9$) representing the number of tasks and duration of the competition, respectively. The second line contains $N$ integers $A_i$ ($1 \le A_i \le 10^9$) representing the time needed by Ayu to solve the $i^{th}$ task. The third line contains $N$ integers $B_i$ ($1 \le B_i \le 10^9$) representing the time needed by Budi to solve the $i^{th}$ task.

# Output

If it is not possible for Ayu to win the competition by having **strictly** larger number of solved tasks than Budi, simply output $-1$ in a line. Otherwise, output begins with an integer $K$ in a line representing the number of balloons Ayu needs to pop. The next line contains $K$ integers (each separated by a single space), sorted by **increasing order**, representing the minute in which Ayu should pop a balloon. You may output any configuration as long as it’s valid, i.e. Ayu has at least one balloon when she pops a balloon, Ayu is not popping a balloon after the contest is over, Ayu is not popping more than one balloon at the same minute, and the configuration causes Ayu to have a larger number of solved tasks than Budi.

# Sample Input #1
```
4 30
9 10 10 10
4 10 5 10
```
# Sample Output #1
```
2
12 19
```
# Explanation for the sample input/output #1

Ayu gets her first balloon at minute 9, while at that time, Budi already solved the first task (at minute 4) and is currently doing his second task which needs 5 more minutes (projected to finish at minute 14). Ayu pops his first balloon at minute 12, i.e. 2 minutes before Budi finish the second task, causing Budi to repeat the second task. Now, the projected time for Budi to finish the second task is at minute 22. At minute 19, Ayu gets her second balloon and pops it right away, causing Budi to repeat the second task again. Now, the projected time for Budi to finish the second task is at minute 29. At minute 29, Ayu gets his third balloon, and at the same time, Budi also solved the second task. The competition ends at minute 30. In total, Ayu solved 3 tasks, while Budi only managed to solve 2 tasks.

# Sample Input #2
```
5 50
10 10 10 10 10
15 12 19 17 20
```
# Sample Output #2
```
0
```
# Sample Input #3
```
5 10
15 10 5 5 5
9 10 10 10 10
```
# Sample Output #3
```
-1
```
# Explanation for the sample input/output #3

Ayu cannot solve the first task during the competition, so no balloon for her to play with.


Sample inputs:
4 30
9 10 10 10
4 10 5 10

5 50
10 10 10 10 10
15 12 19 17 20

5 10
15 10 5 5 5
9 10 10 10 10

Sample outputs:
2
12 19

0

-1

# Solution Analysis

We can easily compute the number of tasks that Ayu is going to solve in the contest time. Let $S$ be this number. Therefore, now Ayu’s goal is to make sure that Budi did not solve the $S^{th}$ problem.

Observe that Ayu can always pop a balloon just before Budi about to solve a task, since if Budi is going to solve the next task in $k$ minutes, it is always not worse to let Budi waste the full $k$ minutes. Therefore, Ayu can decide whether she wants to pop a balloon only every time Budi just about to solve a task.

Suppose Budi is just about to solve a task (let’s say the $x^{th}$ task), how does Ayu determine whether she should pop a balloon? First, if Ayu currently does not have a balloon, then there is no choice but to let Budi solve the task. Otherwise, consider the tasks between task $x$ and task $S$, inclusive. If task $x$ takes the longest to solve for Budi, then Ayu should spend as many balloons as she can on this task. This means Budi will waste as many minutes as Ayu can. If there is a later task (let’s say the $y^{th}$ task) that Budi needs more time to solve, then it is better for Ayu to save her balloon and use it for the $y^{th}$ task instead. This ensures that Budi wastes more time.

Special judge (C++ scorer):
#include <bits/stdc++.h>
using namespace std;

int wa(int status = 0) {
  cout << "WA" << endl;
  cerr << status << endl;
  return 0;
}

int ac() {
  cout << "AC" << endl;
  return 0;
}

bool increasing(const vector<int> &v) {
  for (int i = 1; i < v.size(); ++i) {
    if (v[i - 1] >= v[i]) {
      return false;
    }
  }
  return true;
}

bool timings_ok(int M, const vector<int> &timings) {
  if (!increasing(timings)) {
    return false;
  }
  for (int i = 0; i < timings.size(); ++i) {
    if (timings[i] < 0 || timings[i] > M) {
      return false;
    }
  }
  return true;
}

int checks(istream& tc_in, istream& tc_out, istream& con_out) {
  int N, M;
  tc_in >> N >> M;
  vector<int> A(N), B(N);

  for (int i = 0; i < N; ++i) {
    tc_in >> A[i];
  }

  for (int i = 0; i < N; ++i) {
    tc_in >> B[i];
  }

  int K_out, K_con;
  tc_out >> K_out;

  if (!(con_out >> K_con)) {
    return wa(1);
  }

  if (K_out == -1) {
    if (K_con != -1) {
      return wa(2);
    }
  } else {
    if (K_con < 0 || K_con > N) {
      return wa(3);
    }
    vector<int> timings(K_con);

    for (int i = 0; i < K_con; ++i) {
      if (!(con_out >> timings[i])) {
        return wa(4);
      }
    }

    if (!timings_ok(M, timings)) {
      return wa(5);
    }

    int andi_solved = 0;
    int budi_solved = 0;
    int last_andi_think = 0;
    int last_budi_think = 0;
    for (int i = 0; i < timings.size(); ++i) {
      while (andi_solved < N && last_andi_think + A[andi_solved] <= timings[i]) {
        last_andi_think += A[andi_solved];
        ++andi_solved;
      }
      if ((i + 1) > andi_solved) {
        // Not enough balloon.
        return wa(6);
      }
      while (budi_solved < N && last_budi_think + B[budi_solved] < timings[i]) {
        last_budi_think += B[budi_solved];
        ++budi_solved;
      }
      last_budi_think = timings[i];
    }
    while (andi_solved < N && last_andi_think + A[andi_solved] <= M) {
      last_andi_think += A[andi_solved];
      ++andi_solved;
    }
    while (budi_solved < N && last_budi_think + B[budi_solved] <= M) {
      last_budi_think += B[budi_solved];
      ++budi_solved;
    }

    if (budi_solved >= andi_solved) {
      // Andi not win.
      return wa(7);
    }
  }

  if (con_out >> K_con) {
    return wa(8);
  }
  return ac();
}

int main(int argc, char* argv[]) {
  ifstream tc_in(argv[1]);
  ifstream tc_out(argv[2]);
  ifstream con_out(argv[3]);

  return checks(strcmp(argv[1], "-") == 0 ? cin : tc_in,
                strcmp(argv[2], "-") == 0 ? cin : tc_out,
                strcmp(argv[3], "-") == 0 ? cin : con_out);
}