Your browser (Unknown 0) is no longer supported. Some parts of the website may not work correctly. Please update your browser.

UPCOMING CHALLENGES:

CURRENT CHALLENGES:

Neon 2014

PAST CHALLENGES

Fluorum 2014

Oxygenium 2014

Nitrogenium 2013

Carbo 2013

Boron 2013

Beryllium 2013

Lithium 2013

Helium 2013

Hydrogenium 2013

Omega 2013

Psi 2012

Chi 2012

Phi 2012

Upsilon 2012

Tau 2012

Sigma 2012

Rho 2012

Pi 2012

Omicron 2012

Xi 2012

Nu 2011

Mu 2011

Lambda 2011

Kappa 2011

Iota 2011

Theta 2011

Eta 2011

Zeta 2011

Epsilon 2011

Delta 2011

Gamma 2011

December 2010

PrefixSet edition (2010-10)

Calculate cheapest way of buying gas in order to drive along a highway.

Spoken language:

There are N+1 towns numbered from 0 to N lying along a highway. The distances between towns, and gas prices in each town, are given. A truck has to deliver cargo from town 0 to town N. What is the cheapest way to buy enough gas for this trip?

Two non-empty arrays D (distances) and P (prices), each consisting of N positive integers, and a positive integer T (tank capacity) are given. Consider any sequence R (refill strategy) consisting of N non-negative integers (amount of fuel bought in each town).

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

The following conditions (meaning that the truck must not run out of fuel and cannot fill up with more fuel than its tank capacity) must hold:

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Sequence [15, 10, 2] is a refill strategy with cost 46. Sequence [10, 5, 12] is not a valid refill strategy, because the truck would run out of fuel between towns 1 and 2. Sequence [10, 15, 2] is a refill strategy with cost 41, and no cheaper refill strategy exists for this choice of arrays D, P and number T.

Write a function:

int solution(int D[], int P[], int N, int T);

that, given two non-empty arrays D and P consisting of N positive integers each and a positive integer T, returns the cost of the cheapest refill strategy for D, P and T.

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Copyright 2009–2023 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

There are N+1 towns numbered from 0 to N lying along a highway. The distances between towns, and gas prices in each town, are given. A truck has to deliver cargo from town 0 to town N. What is the cheapest way to buy enough gas for this trip?

Two non-empty arrays D (distances) and P (prices), each consisting of N positive integers, and a positive integer T (tank capacity) are given. Consider any sequence R (refill strategy) consisting of N non-negative integers (amount of fuel bought in each town).

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

The following conditions (meaning that the truck must not run out of fuel and cannot fill up with more fuel than its tank capacity) must hold:

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Sequence [15, 10, 2] is a refill strategy with cost 46. Sequence [10, 5, 12] is not a valid refill strategy, because the truck would run out of fuel between towns 1 and 2. Sequence [10, 15, 2] is a refill strategy with cost 41, and no cheaper refill strategy exists for this choice of arrays D, P and number T.

Write a function:

int solution(vector<int> &D, vector<int> &P, int T);

that, given two non-empty arrays D and P consisting of N positive integers each and a positive integer T, returns the cost of the cheapest refill strategy for D, P and T.

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Copyright 2009–2023 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

There are N+1 towns numbered from 0 to N lying along a highway. The distances between towns, and gas prices in each town, are given. A truck has to deliver cargo from town 0 to town N. What is the cheapest way to buy enough gas for this trip?

Two non-empty arrays D (distances) and P (prices), each consisting of N positive integers, and a positive integer T (tank capacity) are given. Consider any sequence R (refill strategy) consisting of N non-negative integers (amount of fuel bought in each town).

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

The following conditions (meaning that the truck must not run out of fuel and cannot fill up with more fuel than its tank capacity) must hold:

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Sequence [15, 10, 2] is a refill strategy with cost 46. Sequence [10, 5, 12] is not a valid refill strategy, because the truck would run out of fuel between towns 1 and 2. Sequence [10, 15, 2] is a refill strategy with cost 41, and no cheaper refill strategy exists for this choice of arrays D, P and number T.

Write a function:

int solution(vector<int> &D, vector<int> &P, int T);

that, given two non-empty arrays D and P consisting of N positive integers each and a positive integer T, returns the cost of the cheapest refill strategy for D, P and T.

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Copyright 2009–2023 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

class Solution { public int solution(int[] D, int[] P, int T); }

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

int solution(List<int> D, List<int> P, int T);

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

func Solution(D []int, P []int, T int) int

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

class Solution { public int solution(int[] D, int[] P, int T); }

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

class Solution { public int solution(int[] D, int[] P, int T); }

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

function solution(D, P, T);

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

fun solution(D: IntArray, P: IntArray, T: Int): Int

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

function solution(D, P, T)

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Note: All arrays in this task are zero-indexed, unlike the common Lua convention. You can use `#A` to get the length of the array A.

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

int solution(NSMutableArray *D, NSMutableArray *P, int T);

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

function solution(D: array of longint; P: array of longint; N: longint; T: longint): longint;

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

function solution($D, $P, $T);

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

def solution(D, P, T)

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

def solution(d, p, t)

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

object Solution { def solution(d: Array[Int], p: Array[Int], t: Int): Int }

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

public func solution(_ D : inout [Int], _ P : inout [Int], _ T : Int) -> Int

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

function solution(D: number[], P: number[], T: number): number;

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].

Sequence L (amount of fuel when leaving town), consisting of N integers, is defined as follows:

L[K] = (R[0] + ... + R[K]) − (D[0] + ... + D[K−1]) for 0 ≤ K ≤ N−1

Sequence A (amount of fuel when arriving at town), consisting of N integers, is defined as follows:

A[K] = L[K−1] − D[K−1] for 1 ≤ K ≤ N

- 0 ≤ L[K] ≤ T for 0 ≤ K ≤ N−1

- 0 ≤ A[K] ≤ T for 1 ≤ K ≤ N

Number C (total cost of refill strategy) is defined as:

C = R[0] * P[0] + ... + R[N−1] * P[N−1]

For example, consider T = 15 and the following arrays D and P, consisting of three elements each:

Write a function:

Private Function solution(D As Integer(), P As Integer(), T As Integer) As Integer

The function should return −1 if no valid refill strategy exists.

The function should return −2 if the result exceeds 1,000,000,000.

For example, given T = 15 and arrays D and P consisting of three elements each such that:

the function should return 41, as explained above.

Write an ** efficient** algorithm for the following assumptions:

- T is an integer within the range [1..1,000,000];
- N is an integer within the range [1..100,000];
- each element of arrays D and P is an integer within the range [1..1,000,000].