Takahashi Quest

AtCoder
IDabc333_e
Time2000ms
Memory256MB
Difficulty
Takahashi will embark on an adventure. During the adventure, $N$ events will occur. The $i$\-th event $(1\leq i\leq N)$ is represented by a pair of integers $(t _ i,x _ i)$ $(1\leq t _ i\leq 2,1\leq x _ i\leq N)$ and is as follows: * If $t _ i=1$, he finds one potion of type $x _ i$. He can choose to pick it up or discard it. * If $t _ i=2$, he encounters one monster of type $x _ i$. If he has a potion of type $x _ i$, he can use one to defeat the monster. If he does not defeat it, he will be defeated. Determine whether he can defeat all the monsters without being defeated. If he cannot defeat all the monsters, print `-1`. Otherwise, let $K$ be the maximum number of potions he has at some point during the adventure. Let $K _ {\min}$ be the minimum value of $K$ across all strategies where he will not be defeated. Print the value of $K _ {\min}$ and the actions of Takahashi that achieve $K _ {\min}$. ## Constraints * $1\leq N\leq2\times10^5$ * $1\leq t _ i\leq2\ (1\leq i\leq N)$ * $1\leq x _ i\leq N\ (1\leq i\leq N)$ * All input values are integers. ## Input The input is given from Standard Input in the following format: $N$ $t _ 1$ $x _ 1$ $t _ 2$ $x _ 2$ $\vdots$ $t _ N$ $x _ N$ [samples]
Samples
Input #1
13
1 2
1 3
1 1
1 3
1 2
2 3
1 3
1 3
2 3
1 3
2 2
2 3
2 1
Output #1
3
1 1 1 0 0 1 0 1

The sample output corresponds to the following actions:

*   Find potions of types $2,3,1$ in this order. Pick up all of them.
*   Find potions of types $3,2$ in this order. Do not pick up any of them.
*   Encounter a type-$3$ monster. Use one type-$3$ potion to defeat it.
*   Find a type-$3$ potion. Pick it up.
*   Find a type-$3$ potion. Do not pick it up.
*   Encounter a type-$3$ monster. Use one type-$3$ potion to defeat it.
*   Find a type-$3$ potion. Pick it up.
*   Encounter a type-$2$ monster. Use one type-$2$ potion to defeat it.
*   Encounter a type-$3$ monster. Use one type-$3$ potion to defeat it.
*   Encounter a type-$1$ monster. Use one type-$1$ potion to defeat it.

In this sequence of actions, the value of $K$ is $3$.
There is no way to avoid defeat with $K\leq 2$, so the sought value of $K _ {\min}$ is $3$. There are multiple sequences of actions that satisfy $K=3$ and allow him to avoid defeat; you may print any of them.
Input #2
4
2 3
1 4
2 1
1 2
Output #2
\-1

He will inevitably be defeated by the first monster he encounters.
Input #3
30
1 25
1 2
1 10
1 18
2 18
1 11
2 11
1 21
1 6
2 2
2 10
1 11
1 24
1 11
1 3
1 2
1 18
2 25
1 8
1 10
1 11
2 18
2 10
1 10
2 2
1 24
1 10
2 10
1 25
2 6
Output #3
4
1 1 1 1 1 0 1 0 0 0 0 1 1 0 1 0 1 0 0 0
API Response (JSON)
{
  "problem": {
    "name": "Takahashi Quest",
    "description": {
      "content": "Takahashi will embark on an adventure. During the adventure, $N$ events will occur. The $i$\\-th event $(1\\leq i\\leq N)$ is represented by a pair of integers $(t _ i,x _ i)$ $(1\\leq t _ i\\leq 2,1\\leq x",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc333_e"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Takahashi will embark on an adventure.\nDuring the adventure, $N$ events will occur. The $i$\\-th event $(1\\leq i\\leq N)$ is represented by a pair of integers $(t _ i,x _ i)$ $(1\\leq t _ i\\leq 2,1\\leq x...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments