Programming contests

DEIK Regionális Programozó Csapatverseny, egyetemi kategória, 2025. december 7.

December 7, 2025, 10:30 AM – December 7, 2025, 3:30 PM

optimize-2

We model a stock price with the following function: f(x)=A1|B1x|++AN|BNx|f(x)=A_1|B_1-x| + \ldots + A_N|B_N-x| Currently we are at time x=1x=1. We want to know: given a time interval [1,M][1,M], what is the maximum profit (PP) we can achieve by buying and then selling the stock. We can use any times 1xyM1 ≤ x ≤ y ≤ M for buying (xx) and selling (yy). If we don’t buy/sell anything, our profit is 00 of course.

Input specification

All data are integers. In the first line comes TT the number of test cases. Description of each test case: First line contains NN and MM. Next line contains A1,,ANA_1,\ldots,A_N. Third line contains B1,,BNB_1,\ldots,B_N, which are distinct values.

Output specification

TT lines, each containing the requested number PP.

Constraints

1T1001\le T \le 100
1N1001\le N \le 100
1M1_000_000_0001\le M \le 1\_000\_000\_000
1Ak1001\le A_k \le 100
1Bk1_000_000_0001\le B_k \le 1\_000\_000\_000

Sample input 1

  1. 5
  2. 4 10
  3. 1 2 3 4
  4. 1 2 3 4
  5. 9 10
  6. 1 2 3 1 2 3 1 2 3
  7. 1 2 4 8 16 32 64 128 256
  8. 9 50
  9. 1 2 3 1 2 3 1 2 3
  10. 1 2 4 8 16 32 64 128 256
  11. 9 100
  12. 1 2 3 1 2 3 1 2 3
  13. 1 2 4 8 16 32 64 128 256
  14. 9 300
  15. 1 2 3 1 2 3 1 2 3
  16. 1 2 4 8 16 32 64 128 256
download as text file

Sample output 1

  1. 62
  2. 0
  3. 108
  4. 480
  5. 3032
download as text file
University of Debrecen; Faculty of Informatics; v. 09/30/2024