#C11404. Maximum Team Skill Difference Under Constraint

    ID: 40717 Type: Default 1000ms 256MiB

Maximum Team Skill Difference Under Constraint

Maximum Team Skill Difference Under Constraint

You are given an integer ( n ) representing the number of students, an array ( skills ) of ( n ) integers indicating the skill level of each student, and an integer ( T ) representing the maximum allowed total skill score for a team. A team consists of exactly two students. Your task is to choose two students such that their combined skill score is less than or equal to ( T ) and the difference between their skills is maximized. Formally, you need to find ( \max_{i<j,, skills_i+skills_j \leq T} (skills_j - skills_i) ). If no such pair exists, output ( -1 ).


Example:
For input: n = 5, skills = [1, 2, 3, 4, 5], T = 9, one optimal pair is ( (1,5) ) with a difference of 4, so the answer is 4.

inputFormat

The input is read from standard input (stdin) and consists of three lines:

  1. An integer ( n ), representing the number of students.
  2. ( n ) space-separated integers representing the skill levels of the students.
  3. An integer ( T ), the maximum allowed sum of the skills for a valid team.

outputFormat

Output a single integer, which is the maximum skill difference between two team members that satisfies the constraint. If no valid team exists, output ( -1 ). The output should be written to standard output (stdout).## sample

5
1 2 3 4 5
9
4