PROBLEM D: CALCULATE AVERAGE

View as PDF

Submit solution

Points: 50.00
Time limit: 1.0s
Memory limit: 256M
Input: stdin
Output: stdout

Author:
Suggester:
Problem type

Given an integer sequence A consisting of N elements ~a_1, a_2, …, a_N~ and an integer T. Find the triplet of integers ~a_i, a_j, a_k~ (1 ≤ i < j < k ≤ N) whose average difference with the value T is the smallest. If there are multiple sets with the same difference, choose the set with the largest sum of the three numbers.

Input: Input from the standard device has the following format:

  • The first line consists of two integers N and T (3 ≤ N ≤ 5000, |T| ≤ ~10^9~)
  • The second line consists of N integers ~a_1, a_2, …, a_N~ (|~a_i~| ≤ ~10^9~, 1 ≤ i ≤ N).

Output: Write to the standard device a single integer which is the sum of the three numbers found.

Exam:

Input

4 0
-2 3 1 0

Output

1

Explain

The average of any 3 numbers is:

  • (-2, 3, 1): 0.6667
  • (-2, 3 0): 0.3333
  • (-2, 1, 0): -0.3333
  • (3, 1, 0): 1.3333
  • So the triplet satisfying (-2, 3, 0) has a sum of 1

Comments

Please read the guidelines before commenting.


There are no comments at the moment.