There are N cities (numbered from 0 to N-1) located along a road. The K-th city is situated A[K] from the beginning of the road in the west. Cities are numbered in ascending order of position, and no two of them lie in the same place. Formally, A[K] < A[K + 1] holds for every K from 0 to N-2.
The time needed to travel east from city X to the easternmost city equals A[N - 1] - A[X] unless there is a city Y to the east of X (as cars can drive only to the east) with a motorway to the easternmost city built. Then, travel time decreases to A[Y] - A[X] (time spent on the motorway is not considered). If city X has a motorway built, then the travel time from it equals 0.
There are no motorways right now, but one from any city to the easternmost city is planned to be built. Decide where to build it in order to minimize the sum of travel times from every city to the easternmost one.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A of N integers, returns the minimum total travel time as described above.
As the result might be large, return its remainder when divided by 109 + 7.
Examples:
1. Given A = [1, 5, 9, 12], the function should return 7.
- With the motorway from the 0th city the travel times would be: 0 for the 0th city as it has a motorway, 7 for the 1st city and 3 for the 2nd city: that is 10 in total.
- With the motorway from the 1st city the travel times would be: 4 for the 0th city, 0 for the 1st city and 3 for the 2nd city: that is 7 in total.
- With the motorway from the 2nd city the travel times would be: 8 for the 0th city, 4 for the 1st city and 0 for the 2nd city: that is 12 in total.
2. If A = [5, 15], the function should return 0.
We can only build a motorway from the 0th city to the 1st. Travel time from the city to the motorway is 0, so 0 is the answer.
3. If A = [2, 6, 7, 8, 12], the function should return 9.
- With the motorway from the 0th city the total travel time is equal to 0 + 6 + 5 + 4 = 15.
- With the motorway from the 1st city the total travel time is equal to 4 + 0 + 5 + 4 = 13.
- With the motorway from the 2nd city the total travel time is equal to 5 + 1 + 0 + 4 = 10.
- With the motorway from the 3rd city the total travel time is equal to 6 + 2 + 1 + 0 = 9.
The answer is 9, because that is the minimum total time among all motorway placement possibilities.
4. If N = 20 and A[K] = K * (5 * 107) for each K from 0 to 19, the function should return 499999972. The minimal total time among all motorway placement possibilities is 4500000000, whose remainder when divided by 109 + 7 is 499999972.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [2..100,000];
- each element of array A is an integer within the range [0..1,000,000,000];
- the elements of A are all distinct;
- array A is sorted in ascending order.
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long mod = 10^9;
public int solution(int[] A) {
// write your code in Java SE 11
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long mod = 10^9 + 7;
public int solution(int[] A) {
// write your code in Java SE 11
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long mod = 10^9 + 7;
public static long[] fenwick = new long[];
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[];
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
p
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void constructFenwick(int[] A)
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void constructFenwick(int[] A) {
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void constructFenwick(int[] A) {
for(int i=0; i<)
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void constructFenwick(int[] A) {
for(int i=1; i<A.length; i++)
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick()
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i]);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void updateFenwick(int i, int val) {
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i]);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void updateFenwick(int i, int val) {
i = i
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i]);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void updateFenwick(int i, int val) {
i = i + 1;
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i]);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void updateFenwick(int i, int val) {
i = i + 1;
while(i < n)
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i]);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void updateFenwick(int i, int val, int n) {
i = i + 1;
while(i < n)
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void updateFenwick(int i, int val, int n) {
i = i + 1;
while(i <= n) {
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void updateFenwick(int i, int val, int n) {
i = i + 1;
while(i <= n) {
fenwick
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void updateFenwick(int i, int val, int n) {
i = i + 1;
while(i <= n) {
fenwick[i] += val;
i = i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void updateFenwick(int i, int val, int n) {
i = i + 1;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void updateFenwick(int i, int val, int n) {
i = i + 1;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void getSum(int i) {
}
void updateFenwick(int i, int val, int n) {
i = i + 1;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void getSum(int i) {
}
void updateFenwick(int i, int val, int n) {
i = i + 1;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void getSum(int i) {
}
void updateFenwick(int i, int val, int n) {
i = i + 1;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void getSum(int i) {
int sum = 0;
}
void updateFenwick(int i, int val, int n) {
i = i + 1;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void getSum(int i) {
int sum = 0;
i = i + 1;
while(i > 0) {
}
}
void updateFenwick(int i, int val, int n) {
i = i + 1;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
constructFenwick(A)
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick = new long[MAX];
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick()
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
public static long[] fenwick1 = new long[MAX];
public static long[] fenwick2 = new long[MAX];
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick()
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
public static final int MAX = 100_000;
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick()
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public static final long MOD = 10^9 + 7;
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick()
void constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick()
int[] constructFenwick(int[] A) {
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick()
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length]
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick()
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length];
for(int i=0; i<A.length; i++)
updateFenwick(i, A[i], A.length);
return fenwick;
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int i, int val, int n) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick()
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int[] fenwick, int i, int val) {
i++;
while(i <= n) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick()
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int[] fenwick, int i, int val) {
i++;
while(i <= fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick()
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int[] fenwick, int i, int val) {
i++;
while(i <= fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick()
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int[] fenwick, int i, int val) {
i++;
while(i <= fenwick.length - 1) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick()
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int[] fenwick, int i, int val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick()
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int[] fenwick, int i, int val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick() {
}
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int[] fenwick, int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int[] fenwick, int i, int val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
void updateRangeFenwick() {
}
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int[] fenwick, int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int[] fenwick, int i, int val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
void updateRangeFenwick() {
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int[] fenwick, int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int[] fenwick, int i, int val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
void sumFenwick() {
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int[] fenwick, int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int[] fenwick, int i, int val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
void sumFenwick2(int i, int f1[], int f2[]) {
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
void getSum(int[] fenwick, int i) {
int sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
}
void updateFenwick(int[] fenwick, int i, int val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
void sumFenwick2(int i, int f1[], int f2[]) {
return
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(int[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(int[] fenwick, int i, int val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
void sumFenwick2(int i, int f1[], int f2[]) {
return
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(int[] fenwick, int i, int val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
void sumFenwick2(int i, int f1[], int f2[]) {
return
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, int val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
void sumFenwick2(int i, int f1[], int f2[]) {
return
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
int[] constructFenwick(int[] A) {
int[] fenwick = new int[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
void sumFenwick2(int i, int f1[], int f2[]) {
return
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(long[] A) {
int[] fenwick = new int[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
void sumFenwick2(int i, int f1[], int f2[]) {
return
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(long[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
void sumFenwick2(int i, int f1[], int f2[]) {
return
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
void sumFenwick2(int i, int f1[], int f2[]) {
return
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
void sumFenwick2(int i, int f1[], int f2[]) {
return ()
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
void sumFenwick2(int i, int f1[], int f2[]) {
return getSum(f1, i);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
void sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum();
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick()
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[],)
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick()
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val);
updateFenwick(f2, l, val);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return sum(r, f1, f2) - sum(l - 1, f1, f2);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
constructFenwick(A);
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
constructFenwick(A);
for(int i = 0; i < n; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
constructFenwick(A);
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
constructFenwick(A);
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
longconstructFenwick(A);
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
long[] f1 = constructFenwick(A);
long[] f2 = new long[f1.length];
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
long[] f1 = constructFenwick(A);
long[] f2 = new long[f1.length];
long minimal
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
long[] f1 = constructFenwick(A);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
long[] f1 = constructFenwick(A);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
long[] f1 = constructFenwick(A);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
long[] f1 = constructFenwick(A);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
long[] f1 = constructFenwick(A);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = constructFenwick(A);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = constructFenwick(A);
long[] f2 = new long[f1.length];
long minimal = ;
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = constructFenwick(A);
long[] f2 = new long[f1.length];
long minimal = ;
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens
long[] f1 = constructFenwick(A);
long[] f2 = new long[f1.length];
long minimal = ;
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(A);
long[] f2 = new long[f1.length];
long minimal = ;
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = ;
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick();
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, val, 0, i);
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, lens, 0, i);
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, lens[i], 0, i);
l
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2);
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2);
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2);
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(int l, int r, long[] f1, long[] f2) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, );
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, );
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return 0;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return 0;
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 at Solution.getSum(Solution.java:14) at Solution.sumFenwick2(Solution.java:41) at Solution.sumRangeFenwick(Solution.java:53) at Solution.solution(Solution.java:71) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6 at Solution.getSum(Solution.java:14) at Solution.sumFenwick2(Solution.java:41) at Solution.sumRangeFenwick(Solution.java:53) at Solution.solution(Solution.java:71) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 21 out of bounds for length 21 at Solution.getSum(Solution.java:14) at Solution.sumFenwick2(Solution.java:41) at Solution.sumRangeFenwick(Solution.java:53) at Solution.solution(Solution.java:71) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return minimal;
}
}
Solution.java:77: error: incompatible types: possible lossy conversion from long to int return minimal; ^ 1 error
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 at Solution.getSum(Solution.java:14) at Solution.sumFenwick2(Solution.java:41) at Solution.sumRangeFenwick(Solution.java:53) at Solution.solution(Solution.java:71) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6 at Solution.getSum(Solution.java:14) at Solution.sumFenwick2(Solution.java:41) at Solution.sumRangeFenwick(Solution.java:53) at Solution.solution(Solution.java:71) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 21 out of bounds for length 21 at Solution.getSum(Solution.java:14) at Solution.sumFenwick2(Solution.java:41) at Solution.sumRangeFenwick(Solution.java:53) at Solution.solution(Solution.java:71) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = 10^9 + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = Math. + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)Math.exp(10, 9) + 7;
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.exp(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.exp(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
Solution.java:8: error: method exp in class Math cannot be applied to given types; static final long MOD = (long)(Math.exp(10, 9) + 7); ^ required: double found: int,int reason: actual and formal argument lists differ in length 1 error
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
long[] constructFenwick2(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = constructFenwick(lens);
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = new long[A.length + 1]
long[] f2 = new long[f1.length];
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < )
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick();
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
int[] lens = new int[A.length];
for(int i = 0; i < A.length; i++)
lens[i] = A[A.length - 1] - A[i];
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -lens[i], 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A, i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i,);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, lens[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2)) % MOD;
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return (sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], 0, i);
}
return (int)minimal;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], int val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], 0, i);
}
return (int)(minimal % MOD);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], long val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], 0, i);
}
return (int)(minimal % MOD);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], long val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], 0, i);
}
return (int)(minimal % MOD);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
s
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], long val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], 0, i);
}
return (int)(minimal % MOD);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
sum %= MOD;
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return (getSum(f1, i) * i - getSum(f2, i)) % MOD;
}
void updateRangeFenwick(long f1[], long f2[], long val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], 0, i);
}
return (int)(minimal % MOD);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], long val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], 0, i);
}
return (int)(minimal % MOD);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], long val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], 0, i);
}
return (int)(minimal % MOD);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], long val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], 0, i);
}
return (int)(minimal % MOD);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
static final long MOD = (long)(Math.pow(10, 9) + 7);
long getSum(long[] fenwick, int i) {
long sum = 0;
i++;
while(i > 0) {
sum += fenwick[i];
i -= i & (-i);
}
return sum;
}
void updateFenwick(long[] fenwick, int i, long val) {
i++;
while(i < fenwick.length) {
fenwick[i] += val;
i += i & (-i);
}
}
long[] constructFenwick(int[] A) {
long[] fenwick = new long[A.length + 1];
for(int i=0; i<A.length; i++)
updateFenwick(fenwick, i, A[i]);
return fenwick;
}
/* ------------------ */
long sumFenwick2(int i, long f1[], long f2[]) {
return getSum(f1, i) * i - getSum(f2, i);
}
void updateRangeFenwick(long f1[], long f2[], long val, int l, int r) {
updateFenwick(f1, l, val);
updateFenwick(f1, r + 1, -val);
updateFenwick(f2, l, val * (l - 1));
updateFenwick(f2, r + 1, -val * r);
}
long sumRangeFenwick(long[] f1, long[] f2, int l, int r) {
return sumFenwick2(r, f1, f2) - sumFenwick2(l - 1, f1, f2);
}
public int solution(int[] A) {
if(A.length == 2)
return 0;
long[] f1 = new long[A.length + 1];
long[] f2 = new long[A.length + 1];
for(int i = 0; i < A.length; i++)
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], i, i);
long minimal = Long.MAX_VALUE;
for(int i = 0; i < A.length; i++) {
updateRangeFenwick(f1, f2, -(A[A.length - 1] - A[i]), 0, i);
long newLen = sumRangeFenwick(f1, f2, 0, A.length - 1);
minimal = Math.min(minimal, newLen);
updateRangeFenwick(f1, f2, A[A.length - 1] - A[i], 0, i);
}
return (int)(minimal % MOD);
}
}
The solution obtained perfect score.
Test cases where distances between adjacent cities are equal. N <= 100.
Test cases where distances between adjacent cities are non-decreasing. N <= 100.
Test cases where distances between adjacent cities are non-increasing. N <= 100.
Test cases where the result is big and its division remainder has to be returned. N <= 100. Score x 0.5.
Test cases where the result is big and 64-bit variables are needed. N <= 100. Score x 0.5.
Test cases where distances between adjacent cities are equal. N <= 40,000. Score x 2.
Test cases where distances between adjacent cities are non-increasing. N <= 40,000. Score x 2.