#P7700. Fuel Collection Mission

    ID: 20887 Type: Default 1000ms 256MiB

Fuel Collection Mission

Fuel Collection Mission

The courageous Fox Squad is on a mission to collect as much fuel as possible from different planets in the Laila Galaxy. There are \( n \) planets, and for each planet \( i \) the team can collect \( a_i \) units of fuel. However, to travel to planet \( i \) they must spend \( b_i \) units of fuel. Note that fuel is not renewable; if a planet is visited more than once, no additional fuel is collected.

The Fox Squad starts at planet \( P \). They immediately collect the fuel from planet \( P \) and may then travel to other planets in any order. A flight from the current planet to planet \( i \) is allowed as long as the current amount of fuel \( F \) is at least \( b_i \) (i.e. \( F \ge b_i \)); after the flight, the fuel is updated as \( F := F - b_i + a_i \). The squad may choose to stop at any planet (even at \( P \) without leaving it).

The primary objective is to maximize the total fuel available at the end (which is the initial fuel from planet \( P \) plus the net gains from each visited planet). In other words, if the squad visits a set \( S \) of planets (with \( P \) always included), the final fuel will be \[ F = a_P + \sum_{i \in S \setminus \{P\}} (a_i - b_i), \] subject to the condition that the squad is always able to afford the fuel cost for each flight. Among all strategies achieving the maximum final fuel, the squad wishes to maximize the number of distinct planets visited.

Input Format:

  • The first line contains two integers \( n \) and \( P \) (1-indexed), where \( n \) is the number of planets and \( P \) is the starting planet.
  • The second line contains \( n \) integers: \( a_1, a_2, \ldots, a_n \) representing the fuel units available on each planet.
  • The third line contains \( n \) integers: \( b_1, b_2, \ldots, b_n \) representing the fuel cost required to travel to each planet.

Output Format:

  • Output two integers: the maximum final fuel the squad can have and the maximum number of distinct planets they can visit (including the starting planet), while ensuring that after every flight the remaining fuel is non-negative.

Note: The squad can only collect fuel from a planet on its first visit.

inputFormat

The first line contains two integers \( n \) and \( P \): the number of planets and the starting planet index (1-indexed).
The second line contains \( n \) integers representing \( a_1, a_2, \ldots, a_n \), the fuel available on each planet.
The third line contains \( n \) integers representing \( b_1, b_2, \ldots, b_n \), the cost required to travel to each planet.

outputFormat

Output two space-separated integers: the maximum final fuel obtainable and the maximum number of distinct planets visited under an optimal strategy.

sample

5 3
10 5 20 5 8
5 3 6 5 10
27 4