import java.util.*; /** * The maximum number of trips is easy. Stamp all the pages consecutively * until we cannot any more. * To get the minimum number of trips, try counting how many pages would * be filled if a gap of size p_i - 1 is left between each of the previously * stamped pages. To do that we just need to know how many pages were stamped * so far and how many trips we have taken. Use prefix sums. * Time complexity: O(n) * * @author Finn Lidbetter */ public class passport_finn { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long p = sc.nextLong(); long[] vals = new long[n]; for (int i=0; ip and exit early later on. Since 2*p fits // within a long this will work ok. prefixSum[i+1] = prefixSum[i] + vals[i]; } int minTrips = maxTrips; for (int i=0; ip || (vals[i]-1)>p/(i+1)) { minTrips = i; break; } long fill = prefixSum[i] + (i+1)*(vals[i]-1); if (fill>=p) { minTrips = i; break; } } // System.out.println(maxTrips); System.out.println(minTrips); } }