exec_outcome stringclasses 1 value | code_uid stringlengths 32 32 | file_name stringclasses 111 values | prob_desc_created_at stringlengths 10 10 | prob_desc_description stringlengths 63 3.8k | prob_desc_memory_limit stringclasses 18 values | source_code stringlengths 117 65.5k | lang_cluster stringclasses 1 value | prob_desc_sample_inputs stringlengths 2 802 | prob_desc_time_limit stringclasses 27 values | prob_desc_sample_outputs stringlengths 2 796 | prob_desc_notes stringlengths 4 3k ⌀ | lang stringclasses 5 values | prob_desc_input_from stringclasses 3 values | tags listlengths 0 11 | src_uid stringlengths 32 32 | prob_desc_input_spec stringlengths 28 2.37k ⌀ | difficulty int64 -1 3.5k ⌀ | prob_desc_output_spec stringlengths 17 1.47k ⌀ | prob_desc_output_to stringclasses 3 values | hidden_unit_tests stringclasses 1 value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | 0f013ec56c6d65449138df7a3e54ec35 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class ProblemC {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
while (t-- > 0) {
int n = Integer.parseInt(in.readLine());
String[] temp = in.readLine().split(" ");
long[] d = new long[n];
for (int i = 0; i < n; i++) {
d[i] = Long.parseLong(temp[i]);
}
Arrays.sort(d);
long[] diff = new long[n - 1];
for (int i = 0; i < n - 1; i++)
diff[i] = d[i + 1] - d[i];
long cost = d[n - 1];
for (int i = 0; i < n - 1; i++)
cost -= diff[i] * (i + 1) * (n - 1 - i);
// long cost = d[n - 1];
// for (int i = 1; i < n; i++) {
// cost -= (i * (n - i) * (d[i] - d[i - 1]));
// }
System.out.println(cost);
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 2405ed4e00faf5453dc2537e4c087d29 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
import java.io.*;
public class Algorithm {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
// int k = Integer.parseInt(st.nextToken());
long arr[] = new long[n];
long pre[] = new long[n];
long sum = 0;
for (int i = 0; i < n; i++) {
long e = Long.parseLong(st.nextToken());
arr[i] = e;
}
Arrays.sort(arr);
for(int i = 0 ; i < n; i++)
{
sum = sum + arr[i];
pre[i] = sum;
}
int count = 1;
long res = 0;
for(int i = 2; i < n; i++)
{
long to1 = arr[i] * count;
count++;
to1 = to1 - pre[i-2];
res = res + to1;
}
res*=-1;
output.write(res + "\n");
// output.write();
// int n = Integer.parseInt(st.nextToken());
// HashMap<Character, Integer> map = new HashMap<Character, Integer>();
// if
// output.write("YES\n");
// else
// output.write("NO\n");
// long a = Long.parseLong(st.nextToken());
// long b = Long.parseLong(st.nextToken());
// if(flag == 1)
// output.write("NO\n");
// else
// output.write("YES\n" + x + " " + y + " " + z + "\n");
// output.write(n+ "\n");
}
output.flush();
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | b011eec0c62108f6227741a8d57d5825 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
import java.io.*;
public class Algorithm {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
// int k = Integer.parseInt(st.nextToken());
long arr[] = new long[n];
for (int i = 0; i < n; i++) {
long e = Long.parseLong(st.nextToken());
arr[i] = e;
}
Arrays.sort(arr);
long pre[] = new long[n];
pre[0] = arr[0];
for(int i = 1; i < n; i++)
{
pre[i] = pre[i - 1] + arr[i];
}
if(n<=2)
output.write("0\n");
else
{
int count = 1;
long res = 0;
for(int i = 2; i < n; i++)
{
res+= (arr[i] * count) - pre[i-2];
count++;
}
res*=-1;
output.write(res + "\n");
}
// output.write();
// int n = Integer.parseInt(st.nextToken());
// HashMap<Character, Integer> map = new HashMap<Character, Integer>();
// if
// output.write("YES\n");
// else
// output.write("NO\n");
// long a = Long.parseLong(st.nextToken());
// long b = Long.parseLong(st.nextToken());
// if(flag == 1)
// output.write("NO\n");
// else
// output.write("YES\n" + x + " " + y + " " + z + "\n");
// output.write(n+ "\n");
}
output.flush();
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 20292218f92504ad161101970c4c37b6 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
public class C_Great_Graphs {
private static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static ArrayList<Integer> primeNumbers(boolean[] isPrime) {
Arrays.fill(isPrime, true);
ArrayList<Integer> primes = new ArrayList<>();
isPrime[0] = isPrime[1] = false;
for(int i=2; i<isPrime.length; i++) {
for(int j=i*i; j<isPrime.length; j+=i) {
if(j>0)
isPrime[j] = false;
else
break;
}
}
for(int i=2; i<isPrime.length; i++) {
if(isPrime[i] == true) {
primes.add(i);
}
}
return primes;
}
private static FastReader sc = new FastReader();
public static void main(String[] args) {
int t = sc.nextInt();
while(t-->0) solve();
}
private static long mod = (int)1e9+7;
private static void solve(){
int n = sc.nextInt();
long a[] = new long[n];
a[0] = sc.nextInt();
long neg[] = new long[n];
neg[0] = 0;
long ans = 0;
for(int i=1; i<n; i++){
a[i] = sc.nextLong();
}
Arrays.sort(a);
for(int i=1; i<n; i++){
neg[i] = neg[i-1]+(long)i*(a[i]-a[i-1]);
ans=ans-neg[i];
}
ans+=a[n-1];
System.out.println(ans);
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 9a2e422cf69363e14073736976554929 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class M{
private static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter w=new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int t=Integer.parseInt(br.readLine());
while(t>0) {
int n=Integer.parseInt(br.readLine());
long[] arr=new long[n];
StringTokenizer str=new StringTokenizer(br.readLine());
long maximum=Long.MIN_VALUE;
for(int i=0;i<n;i++) {
arr[i]=Long.parseLong(str.nextToken());
if(arr[i]>maximum) {
maximum=arr[i];
}
}
if(n==1) {
w.write(0+"\n");
}
else if(n==2) {
w.write(0+"\n");
}
else {
sort(arr, 1, n-1);
long[] narr=new long[n-1];
for(int i=1;i<n;i++) {
narr[i-1]=arr[i]-arr[i-1];
}
int sz=n-1;
int i=sz-2;
int j=sz-1;
int count=0;
long prevRes=0;
long sum=0;
while(i>=0){
long temp=narr[i]+narr[j];
long temp2=narr[i]*count+prevRes;
prevRes=temp+temp2;
sum+=prevRes;
i--;
count++;
j--;
}
w.write(-sum+"\n");
}
// g.displayGraph();
t--;
}
w.flush();
w.close();
}
public static void merge(long arr[], int l, int m, int r){
int n1=m-l+1;
int n2=r-m;
long L[] = new long[n1];
long R[] = new long[n2];
for (int i = 0; i < n1; ++i) {
L[i] = arr[l + i];
}
for (int j = 0; j < n2; ++j) {
R[j] = arr[m + 1 + j];
}
int i=0,j=0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
public static void sort(long arr[], int l, int r){
if (l < r) {
int m =l+ (r-l)/2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | f47507a11a149d799fd7fbd99faa9136 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class M{
private static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter w=new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int t=Integer.parseInt(br.readLine());
while(t>0) {
int n=Integer.parseInt(br.readLine());
long[] arr=new long[n];
StringTokenizer str=new StringTokenizer(br.readLine());
long maximum=Long.MIN_VALUE;
for(int i=0;i<n;i++) {
arr[i]=Long.parseLong(str.nextToken());
if(arr[i]>maximum) {
maximum=arr[i];
}
}
if(n==1) {
w.write(0+"\n");
}
else if(n==2) {
w.write(0+"\n");
}
else {
sort(arr, 0, n-1);
long sum=0;
for (int i = n - 1; i >= 0; i--) {
sum += (i*arr[i])-((n-1-i)*arr[i]);
}
long res=sum-arr[n-1];
w.write(-res+"\n");
}
// g.displayGraph();
t--;
}
w.flush();
w.close();
}
public static void merge(long arr[], int l, int m, int r){
int n1=m-l+1;
int n2=r-m;
long L[] = new long[n1];
long R[] = new long[n2];
for (int i = 0; i < n1; ++i) {
L[i] = arr[l + i];
}
for (int j = 0; j < n2; ++j) {
R[j] = arr[m + 1 + j];
}
int i=0,j=0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
public static void sort(long arr[], int l, int r){
if (l < r) {
int m =l+ (r-l)/2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | b2c058340eab872b3c6d529570ada0f7 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
import java.io.*;
public class B{
public static void main(String[] args)
{
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
for(int tt=0;tt<t;tt++)
{
int n = fs.nextInt();
int[] arr = fs.readArray(n);
arr = sort(arr);
long ans = 0;
for(int i=0;i<n-1;i++)
{
ans -= (long)(arr[i+1]-arr[i])*(long)((long)(i+1)*(long)(n-i-1));
ans += (long)(arr[i+1] - arr[i]);
}
out.println(ans);
}
out.close();
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
public static int[] sort(int[] arr)
{
List<Integer> temp = new ArrayList();
for(int i:arr)temp.add(i);
Collections.sort(temp);
int start = 0;
for(int i:temp)arr[start++]=i;
return arr;
}
public static String rev(String str)
{
char[] arr = str.toCharArray();
char[] ret = new char[arr.length];
int start = arr.length-1;
for(char i:arr)ret[start--] = i;
String ret1 = "";
for(char ch:ret)ret1+=ch;
return ret1;
// return ret;
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 11267baf4d5f67ad3e7708cec984bb9c | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class hacker49 {
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
OutputStream outputStream =System.out;
PrintWriter out =new PrintWriter(outputStream);
FastReader s=new FastReader();
int t=s.nextInt();
while(t>0) {
int n=s.nextInt();
long[] a=new long[n];
for(int i=0;i<n;i++) {
a[i]=s.nextLong();
}
Arrays.sort(a);
long[] b=new long[n-1];
for(int i=1;i<n;i++) {
b[i-1]=a[i]-a[i-1];
}
long ans=0;
for(long i=0;i<n-1;i++) {
ans+=(b[(int) i]);
// if(b[i]>0) {
ans-=(long)(b[(int) i]*((i+1)*(n-i-1)));
// }
}
out.println(ans);
t--;
}
out.close();
}
public static int lower_bound(ArrayList<Long> a ,int n,long x) {
int l=0;
int r=n;
while(r>l+1) {
int mid=(l+r)/2;
if(a.get(mid)<=x) {
l=mid;
}else {
r=mid;
}
}
return l;
}
public static int[] is_prime=new int[1000001];
public static ArrayList<Long> primes=new ArrayList<>();
public static void sieve() {
long maxN=1000000;
for(long i=1;i<=maxN;i++) {
is_prime[(int) i]=1;
}
is_prime[0]=0;
is_prime[1]=0;
for(long i=2;i*i<=maxN;i++) {
if(is_prime[(int) i]==1) {
// primes.add((int) i);
for(long j=i*i;j<=maxN;j+=i) {
is_prime[(int) j]=0;
}
}
}
for(long i=0;i<=maxN;i++) {
if(is_prime[(int) i]==1) {
primes.add(i);
}
}
}
public static long[] merge_sort(long[] A, int start, int end) {
if (end > start) {
int mid = (end + start) / 2;
long[] v = merge_sort(A, start, mid);
long[] o = merge_sort(A, mid + 1, end);
return (merge(v, o));
} else {
long[] y = new long[1];
y[0] = A[start];
return y;
}
}
public static long[] merge(long a[], long b[]) {
long[] temp = new long[a.length + b.length];
int m = a.length;
int n = b.length;
int i = 0;
int j = 0;
int c = 0;
while (i < m && j < n) {
if (a[i] < b[j]) {
temp[c++] = a[i++];
} else {
temp[c++] = b[j++];
}
}
while (i < m) {
temp[c++] = a[i++];
}
while (j < n) {
temp[c++] = b[j++];
}
return temp;
}
public static long im(long a) {
return binary_exponentiation_1(a,mod-2)%mod;
}
public static long binary_exponentiation_1(long a,long n) {
long res=1;
while(n>0) {
if(n%2!=0) {
res=((res)%(1000000007) * (a)%(1000000007))%(1000000007);
n--;
}else {
a=((a)%(1000000007) *(a)%(1000000007))%(1000000007);
n/=2;
}
}
return (res)%(1000000007);
}
public static long[] fac=new long[100001];
public static void find_factorial() {
fac[0]=1;
fac[1]=1;
for(int i=2;i<=100000;i++) {
fac[i]=(fac[i-1]*i)%(mod);
}
}
static long mod=1000000007;
public static long GCD(long a,long b) {
if(b==(long)0) {
return a;
}
return GCD(b , a%b);
}
static long c=0;
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 01050444c26d18d8c5ea60f9bb59db14 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class hacker49 {
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
OutputStream outputStream =System.out;
PrintWriter out =new PrintWriter(outputStream);
FastReader s=new FastReader();
int t=s.nextInt();
while(t>0) {
int n=s.nextInt();
long[] a=new long[n];
for(int i=0;i<n;i++) {
a[i]=s.nextLong();
}
// long
long[] b=merge_sort(a,0,n-1);
long sum=0;
long ans=0;
for(int i=2;i<n;i++){
sum+=b[i-2];
ans-=(b[i]*(i-1));
ans+=sum;
}
out.println(ans);
t--;
}
out.close();
}
public static int lower_bound(ArrayList<Long> a ,int n,long x) {
int l=0;
int r=n;
while(r>l+1) {
int mid=(l+r)/2;
if(a.get(mid)<=x) {
l=mid;
}else {
r=mid;
}
}
return l;
}
public static int[] is_prime=new int[1000001];
public static ArrayList<Long> primes=new ArrayList<>();
public static void sieve() {
long maxN=1000000;
for(long i=1;i<=maxN;i++) {
is_prime[(int) i]=1;
}
is_prime[0]=0;
is_prime[1]=0;
for(long i=2;i*i<=maxN;i++) {
if(is_prime[(int) i]==1) {
// primes.add((int) i);
for(long j=i*i;j<=maxN;j+=i) {
is_prime[(int) j]=0;
}
}
}
for(long i=0;i<=maxN;i++) {
if(is_prime[(int) i]==1) {
primes.add(i);
}
}
}
public static long[] merge_sort(long[] A, int start, int end) {
if (end > start) {
int mid = (end + start) / 2;
long[] v = merge_sort(A, start, mid);
long[] o = merge_sort(A, mid + 1, end);
return (merge(v, o));
} else {
long[] y = new long[1];
y[0] = A[start];
return y;
}
}
public static long[] merge(long a[], long b[]) {
long[] temp = new long[a.length + b.length];
int m = a.length;
int n = b.length;
int i = 0;
int j = 0;
int c = 0;
while (i < m && j < n) {
if (a[i] < b[j]) {
temp[c++] = a[i++];
} else {
temp[c++] = b[j++];
}
}
while (i < m) {
temp[c++] = a[i++];
}
while (j < n) {
temp[c++] = b[j++];
}
return temp;
}
public static long im(long a) {
return binary_exponentiation_1(a,mod-2)%mod;
}
public static long binary_exponentiation_1(long a,long n) {
long res=1;
while(n>0) {
if(n%2!=0) {
res=((res)%(1000000007) * (a)%(1000000007))%(1000000007);
n--;
}else {
a=((a)%(1000000007) *(a)%(1000000007))%(1000000007);
n/=2;
}
}
return (res)%(1000000007);
}
public static long[] fac=new long[100001];
public static void find_factorial() {
fac[0]=1;
fac[1]=1;
for(int i=2;i<=100000;i++) {
fac[i]=(fac[i-1]*i)%(mod);
}
}
static long mod=1000000007;
public static long GCD(long a,long b) {
if(b==(long)0) {
return a;
}
return GCD(b , a%b);
}
static long c=0;
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | e9ffc04c1adfd99644b5030aad6c5385 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main extends PrintWriter {
static BufferedReader s = new BufferedReader(new InputStreamReader(System.in));Main () { super(System.out); }public static void main(String[] args) throws IOException{ Main d1=new Main ();d1.main();d1.flush(); }
void main() throws IOException {
StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder();
int t = 1;
t = i(s()[0]);
while (t-- > 0) {
String[] s1 = s();
int n = i(s1[0]);
int [] a= new int[n];
arri(a, n);
Arrays.sort(a);
long count = 0;
long sum = 0;
long ans = 0;
long prev = 0;
for(int i=0;i<n;i++){
sum += a[i];
ans = ans - ((long)(i + 1) * (long)a[i] - sum);
}
ans += a[n - 1];
sb.append(ans + "\n");
}
System.out.println(sb);
} static long powerwithmod(long x, long y, long p) {
long res = 1;
x = x % p;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static String[] s() throws IOException { return s.readLine().trim().split("\\s+"); }
static int i(String ss) {return Integer.parseInt(ss); }
static long l(String ss) {return Long.parseLong(ss); }
public void arr(long[] a,int n) throws IOException {String[] s2=s();for(int i=0;i<n;i++){ a[i]=l(s2[i]); }}
public void arri(int[] a,int n) throws IOException {String[] s2=s();for(int i=0;i<n;i++){ a[i]=(i(s2[i])); }}
}class Pair{
int i, val;long sum1 ;
public Pair(int i, int val, long ss){
this.i = i;sum1 = ss;
this.val = val;
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 450b27d3ee2d3e619906e228313459d1 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class Sample {
public static void main(String[] args) {
ReadInput in = new ReadInput();
int T = in.nextInt();
while(T-->0) {
int n = in.nextInt();
int[] are = in.readArray(n);
Sort.mergeSort(are, 0, n-1);
long[] suff = new long[n];
suff[0]=0;
for(int i=1; i<n; i++) {
suff[i] = suff[i-1]+are[i];
}
long ans = 0;
if(n<2) {
System.out.println(0);
continue;
}
int last = are[1];
int index=1;
for(; index<n && are[index]==last; index++) {}
int count = 0;
for(int i=index; i<n; i++) {
if(are[index-1]==are[index]) count++;
else count=0;
ans -= ((long)(i-2-count)*(long)are[i]-(long)suff[i-2-count])+(long)are[i];
}
System.out.println(ans);
}
}
static class ReadInput {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
class TreeNode {
int key, height;
TreeNode left, right;
TreeNode(int x) {
this.key = x; this.height = 1;
this.left=null; this.right=null;
}
}
class BST {
TreeNode root;
TreeNode insert(TreeNode root, int key) {
if (root == null) {
root = new TreeNode(key);
return root;
}
if (key < root.key) root.left = insert(root.left, key);
else if (key > root.key) root.right = insert(root.right, key);
return root;
}
TreeNode search(TreeNode root, int key) {
if (root==null || root.key==key) return root;
if (root.key < key) return search(root.right, key);
return search(root.left, key);
}
}
class AVL {
TreeNode root;
int height(TreeNode node) {
if (node == null) return 0;
return node.height;
}
TreeNode rightRotate(TreeNode y) {
TreeNode x = y.left; TreeNode subtree = x.right;
x.right = y; y.left = subtree;
y.height = Math.max(height(y.left), height(y.right))+1;
x.height = Math.max(height(x.left), height(x.right))+1;
return x;
}
TreeNode leftRotate(TreeNode x) {
TreeNode y = x.right; TreeNode subtree = y.left;
y.left = x; x.right = subtree;
x.height = Math.max(height(x.left), height(x.right))+1;
y.height = Math.max(height(y.left), height(y.right))+1;
return y;
}
int balanceCount(TreeNode node) {
if (node == null) return 0;
return height(node.left) - height(node.right);
}
TreeNode insert(TreeNode node, int key) {
if (node == null) return (new TreeNode(key));
if (key < node.key) node.left = insert(node.left, key);
else if (key > node.key) node.right = insert(node.right, key);
else return node;
node.height = 1+Math.max(height(node.left), height(node.right));
int count = balanceCount(node);
if (count > 1) {
if (key < node.left.key) return rightRotate(node);
node.left = leftRotate(node.left); return rightRotate(node);
}
if (count < -1) {
if(key > node.right.key) return leftRotate(node);
node.right = rightRotate(node.right); return leftRotate(node);
}
return node;
}
}
class ListNode {
int key;
ListNode next;
ListNode(int x) { this.key = x; this.next = null; }
}
class Traversals {
static void postorder(TreeNode node) {
if (node == null) return; postorder(node.left);
postorder(node.right); System.out.print(node.key+" ");
}
static void inorder(TreeNode node) {
if (node == null) return; inorder(node.left);
System.out.print(node.key+" "); inorder(node.right);
}
static void preorder(TreeNode node) {
if (node == null) return; System.out.print(node.key+" ");
preorder(node.left); preorder(node.right);
}
}
class NT {
static int gcd(int x, int y) {
if (x == 0) return y; if (y == 0) return x;
if (x == y) return x; if (x > y) return gcd(x-y, y);
return gcd(x, y-x);
}
static boolean[] sievePrime(int n) {
boolean isPrime[]=new boolean[n+1]; for(int i=0;i<=n;i++) isPrime[i]=true;
for(int i=2;i*i<=n;i++) if(isPrime[i]==true) for(int j=i*i;j<=n;j+=i) isPrime[j]=false;
return isPrime;
}
}
class Sort {
static void mergeSortHelper(int are[], int l, int m, int r)
{
int n1 = m - l + 1, n2 = r - m;
int[] left = new int[n1];
int[] right = new int[n2];
for (int i = 0; i < n1; ++i) left[i] = are[l + i];
for (int j = 0; j < n2; ++j) right[j] = are[m + 1 + j];
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (left[i] <= right[j]) {
are[k] = left[i]; i++;
}
else {
are[k] = right[j]; j++;
}
k++;
}
while (i < n1) {
are[k] = left[i]; i++; k++;
}
while (j < n2) {
are[k] = right[j]; j++; k++;
}
}
static void mergeSort(int are[], int l, int r)
{
if (l<r) {
int m=l+(r-l)/2;
mergeSort(are, l, m); mergeSort(are, m + 1, r);
mergeSortHelper(are, l, m, r);
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | f95d171e65bd48dd09380efbb73e83eb | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.util.*;
import java.io.*;
public class three3 {
static int n;
static long[] arr;
// static ArrayList<A>[] g;
static long ans;
public static void main(String[] args) throws IOException, FileNotFoundException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader in = new BufferedReader(new FileReader("three"));
int t = Integer.parseInt(in.readLine());
while (t-- > 0) {
n = Integer.parseInt(in.readLine());
arr = new long[n];
// g = new ArrayList[n];
ans = 0;
StringTokenizer st = new StringTokenizer(in.readLine());
for (int i=0; i<n; i++) {
arr[i] = Long.parseLong(st.nextToken());
// g[i] = new ArrayList<>();
}
long sum = 0;
Arrays.parallelSort(arr);
for (int i=1; i<n; i++) {
ans += arr[i] * (i) - sum;
sum += arr[i];
ans -= (arr[i] - arr[i-1]);
}
System.out.println(-ans);
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | c6d741c9196410424ec8c3a6b7b0c229 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
public class GreatestGraphs {
private static long solve(int n, int[] d, int[] a, long sum) {
long s[] = new long[n];
long dp[] = new long[n];
dp[0] = -sum;
s[0] = -sum;
for (int i = 1; i < n; i++) {
dp[i] = dp[i - 1] + (long) a[i] * (long) (n - i);
s[i] = s[i - 1] + dp[i] + (long) a[i];
}
return s[n - 1];
}
public static void main(String[] args)
throws IOException {
Scanner s = new Scanner();
int t = 1;
t = s.nextInt();
StringBuilder ans = new StringBuilder();
int count = 0;
while (t-- > 0) {
int n = s.nextInt();
int d[] = new int[n];
int a[] = new int[n];
long sum = 0;
for (int i = 0; i < n; i++) {
d[i] = s.nextInt();
sum += d[i];
}
Arrays.parallelSort(d);
for (int i = 1; i < n; i++) a[i] = d[i] - d[i - 1];
ans.append(solve(n, d, a, sum)).append("\n");
}
System.out.println(ans.toString());
}
static class Scanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Scanner() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Scanner(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public static long norm(long a, long MOD) {
return ((a % MOD) + MOD) % MOD;
}
public static long msub(long a, long b, long MOD) {
return norm(norm(a, MOD) - norm(b, MOD), MOD);
}
public static long madd(long a, long b, long MOD) {
return norm(norm(a, MOD) + norm(b, MOD), MOD);
}
public static long mMul(long a, long b, long MOD) {
return norm(norm(a, MOD) * norm(b, MOD), MOD);
}
public static long mDiv(long a, long b, long MOD) {
return norm(norm(a, MOD) / norm(b, MOD), MOD);
}
public static String formattedArray(int a[]) {
StringBuilder res = new StringBuilder("");
for (int e : a)
res.append(e).append(" ");
return res.toString().trim();
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 66d8d7554bb5b8bd4e5bb40af7018b37 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
public class GreatestGraphs {
private static long solve(int n, int[] d, int[] a, long sum) {
long s[] = new long[n];
long dp[] = new long[n];
dp[0] = -sum;
s[0] = -sum;
for (int i = 1; i < n; i++) {
dp[i] = dp[i - 1] + (long) a[i] * (long) (n - i);
s[i] = s[i - 1] + dp[i] + (long) a[i];
}
return s[n - 1];
}
public static void main(String[] args)
throws IOException {
Scanner s = new Scanner();
int t = 1;
t = s.nextInt();
StringBuilder ans = new StringBuilder();
int count = 0;
while (t-- > 0) {
int n = s.nextInt();
int d[] = new int[n];
int a[] = new int[n];
long sum = 0;
for (int i = 0; i < n; i++) {
d[i] = s.nextInt();
sum += d[i];
}
Arrays.sort(d);
for (int i = 1; i < n; i++) a[i] = d[i] - d[i - 1];
ans.append(solve(n, d, a, sum)).append("\n");
}
System.out.println(ans.toString());
}
static class Scanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Scanner() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Scanner(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public static long norm(long a, long MOD) {
return ((a % MOD) + MOD) % MOD;
}
public static long msub(long a, long b, long MOD) {
return norm(norm(a, MOD) - norm(b, MOD), MOD);
}
public static long madd(long a, long b, long MOD) {
return norm(norm(a, MOD) + norm(b, MOD), MOD);
}
public static long mMul(long a, long b, long MOD) {
return norm(norm(a, MOD) * norm(b, MOD), MOD);
}
public static long mDiv(long a, long b, long MOD) {
return norm(norm(a, MOD) / norm(b, MOD), MOD);
}
public static String formattedArray(int a[]) {
StringBuilder res = new StringBuilder("");
for (int e : a)
res.append(e).append(" ");
return res.toString().trim();
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 9f303f5de75a6e3203e561fc379ef99b | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
public class GreatestGraphs {
private static long solve(int n, int[] d, int[] a, long sum) {
long s[] = new long[n];
long dp[] = new long[n];
dp[0] = -sum;
s[0] = -sum;
for (int i = 1; i < n; i++) {
dp[i] = dp[i - 1] + (long) a[i] * (long) (n - i);
s[i] = s[i - 1] + dp[i] + (long) a[i];
}
return s[n - 1];
}
public static void main(String[] args)
throws IOException {
Scanner s = new Scanner();
int t = 1;
t = s.nextInt();
StringBuilder ans = new StringBuilder();
int count = 0;
while (t-- > 0) {
int n = s.nextInt();
int d[] = new int[n];
int a[] = new int[n];
long sum = 0;
for (int i = 0; i < n; i++) {
d[i] = s.nextInt();
sum += d[i];
}
Arrays.sort(d);
for (int i = 1; i < n; i++) a[i] = d[i] - d[i - 1];
ans.append(solve(n, d, a, sum)).append("\n");
}
System.out.println(ans.toString());
}
static class Scanner {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Scanner() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Scanner(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
public static long norm(long a, long MOD) {
return ((a % MOD) + MOD) % MOD;
}
public static long msub(long a, long b, long MOD) {
return norm(norm(a, MOD) - norm(b, MOD), MOD);
}
public static long madd(long a, long b, long MOD) {
return norm(norm(a, MOD) + norm(b, MOD), MOD);
}
public static long mMul(long a, long b, long MOD) {
return norm(norm(a, MOD) * norm(b, MOD), MOD);
}
public static long mDiv(long a, long b, long MOD) {
return norm(norm(a, MOD) / norm(b, MOD), MOD);
}
public static String formattedArray(int a[]) {
StringBuilder res = new StringBuilder("");
for (int e : a)
res.append(e).append(" ");
return res.toString().trim();
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 45eca785fe832bbf69cf939bc58b1958 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | /*
created by: nitin23329
on Date: 09/06/21
*/
import java.io.*;
import java.util.*;
public class CodeForces {
/*****************************************WRITING CODE STARTS HERE******************************************/
private static void solve(int test) {
int n = sc.nextInt();
long [] arr = sc.set_long_array(n);
sort(arr,true);
long prefix = 0;
long ans = 0;
long diff = 0;
for(int i=0;i<n;i++){
prefix += arr[i];
long curr = (i+1L)*arr[i] - prefix;
if(curr>=0)ans +=curr;
if(i>0)diff += arr[i] - arr[i-1];
}
out.println(-ans +diff);
}
/*****************************************WRITING CODE ENDS HERE******************************************/
public static void main(String[] args) {
FIO();
int testCase = 1;
testCase = sc.nextInt();
for (int i = 1; i <= testCase; i++) solve(i);
// out.println(((System.currentTimeMillis()) - start )/1000d);
closeIO();
}
static Scanner sc; // FAST INPUT
static PrintWriter out; // FAST OUTPUT
/**************************************HELPER FUNCTION STARTS HERE ************************************/
public static int mod = (int) 1e9 + 7;
public static final int inf_int = (int) 1e8;
public static final long inf_long = (long) 1e14;
public static final String YES = "YES";
public static final String NO = "NO";
public static void sort(int[] a, boolean isAscending) {
ArrayList<Integer> temp = new ArrayList<>();
for (int j : a) temp.add(j);
sort(temp, isAscending);
for (int i = 0; i < a.length; i++) a[i] = temp.get(i);
}
public static void sort(long[] a, boolean isAscending) {
ArrayList<Long> temp = new ArrayList<>();
for (long ele : a) temp.add(ele);
sort(temp, isAscending);
for (int i = 0; i < a.length; i++) a[i] = temp.get(i);
}
public static void sort(List list, boolean isAscending) {
if (isAscending)
Collections.sort(list);
else Collections.sort(list, Collections.reverseOrder());
}
// count the length of a number, time O(1)
public static int countDigitBase10(long n) {
if (n == 0) return 0;
return (int) (1 + Math.log10(n));
}
// euclidean algorithm
public static long gcd(long a, long b) {
// time O(max (loga ,logb))
long x = Math.min(a, b);
long y = Math.max(a, b);
if (y % x == 0) return x;
return gcd(y % x, x);
}
public static long lcm(long a, long b) {
// lcm(a,b) * gcd(a,b) = a * b
return (a / gcd(a, b)) * b;
}
public static long power(long x, long n) {
// time O(logn)
long ans = 1;
while (n > 0) {
if ((n & 1) == 1) {
ans *= x;
ans %= mod;
n--;
} else {
x *= x;
x %= mod;
n >>= 1;
}
}
return ans;
}
public static long firstDivisor(long n) {
if (n == 1 || n == 0) return n;
for (long i = 2; i * i <= n; i++)
if (n % i == 0) return i;
return -1;
}
public static long max3(long a, long b, long c) {
return max2(max2(a, b), c);
}
public static long max2(long a, long b) {
return Math.max(a, b);
}
public static long min3(long a, long b, long c) {
return min2(min2(a, b), c);
}
public static long min2(long a, long b) {
return Math.min(a, b);
}
public static int max3(int a, int b, int c) {
return max2(max2(a, b), c);
}
public static int max2(int a, int b) {
return Math.max(a, b);
}
public static int min3(int a, int b, int c) {
return min2(min2(a, b), c);
}
public static int min2(int a, int b) {
return Math.min(a, b);
}
/**************************************HELPER FUNCTION ENDS HERE ************************************/
/*****************************************FAST INPUT STARTS HERE *************************************/
public static void FIO() {
sc = new Scanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
}
public static void closeIO() {
out.flush();
out.close();
}
private static class Scanner {
private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] set_int_array(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = nextInt();
return arr;
}
public long[] set_long_array(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) arr[i] = nextLong();
return arr;
}
public double[] set_double_array(int n) {
double[] arr = new double[n];
for (int i = 0; i < n; i++) arr[i] = sc.nextDouble();
return arr;
}
public int[][] set_2D_int_array(int n) {
return set_2D_int_array(n, n);
}
public int[][] set_2D_int_array(int row, int col) {
int[][] arr = new int[row][col];
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
arr[i][j] = nextInt();
return arr;
}
public long[][] set_2D_long_array(int n) {
return set_2D_long_array(n, n);
}
public long[][] set_2D_long_array(int row, int col) {
long[][] arr = new long[row][col];
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
arr[i][j] = nextLong();
return arr;
}
public char[][] set_2D_char_array(int n) {
return set_2D_char_array(n, n);
}
public char[][] set_2D_char_array(int row, int col) {
char[][] ch = new char[row][col];
for (int i = 0; i < row; i++)
ch[i] = sc.next().toCharArray();
return ch;
}
}
/*****************************************FAST INPUT ENDS HERE *************************************/
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 09a72f88ae1ea5af3803a93d357dc7dd | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | /*
created by: nitin23329
on Date: 09/06/21
*/
import java.io.*;
import java.util.*;
public class CodeForces {
/*****************************************WRITING CODE STARTS HERE******************************************/
private static void solve(int test) {
int n = sc.nextInt();
int[] arr = sc.set_int_array(n);
sort(arr,true);
long ans = 0;
long prefix = 0;
for(int i=0;i<n;i++){
long curr = (long) i * arr[i] - prefix;
ans -= curr;
if(i>0)ans += arr[i] - arr[i-1];
prefix += arr[i];
}
out.println(ans);
}
/*****************************************WRITING CODE ENDS HERE******************************************/
public static void main(String[] args) {
FIO();
int testCase = 1;
testCase = sc.nextInt();
for (int i = 1; i <= testCase; i++) solve(i);
// out.println(((System.currentTimeMillis()) - start )/1000d);
closeIO();
}
static Scanner sc; // FAST INPUT
static PrintWriter out; // FAST OUTPUT
/**************************************HELPER FUNCTION STARTS HERE ************************************/
public static int mod = (int) 1e9 + 7;
public static final int inf_int = (int) 1e8;
public static final long inf_long = (long) 1e14;
public static final String YES = "YES";
public static final String NO = "NO";
public static void sort(int[] a, boolean isAscending) {
ArrayList<Integer> temp = new ArrayList<>();
for (int j : a) temp.add(j);
sort(temp, isAscending);
for (int i = 0; i < a.length; i++) a[i] = temp.get(i);
}
public static void sort(long[] a, boolean isAscending) {
ArrayList<Long> temp = new ArrayList<>();
for (long ele : a) temp.add(ele);
sort(temp, isAscending);
for (int i = 0; i < a.length; i++) a[i] = temp.get(i);
}
public static void sort(List list, boolean isAscending) {
if (isAscending)
Collections.sort(list);
else Collections.sort(list, Collections.reverseOrder());
}
// count the length of a number, time O(1)
public static int countDigitBase10(long n) {
if (n == 0) return 0;
return (int) (1 + Math.log10(n));
}
// euclidean algorithm
public static long gcd(long a, long b) {
// time O(max (loga ,logb))
long x = Math.min(a, b);
long y = Math.max(a, b);
if (y % x == 0) return x;
return gcd(y % x, x);
}
public static long lcm(long a, long b) {
// lcm(a,b) * gcd(a,b) = a * b
return (a / gcd(a, b)) * b;
}
public static long power(long x, long n) {
// time O(logn)
long ans = 1;
while (n > 0) {
if ((n & 1) == 1) {
ans *= x;
ans %= mod;
n--;
} else {
x *= x;
x %= mod;
n >>= 1;
}
}
return ans;
}
public static long firstDivisor(long n) {
if (n == 1 || n == 0) return n;
for (long i = 2; i * i <= n; i++)
if (n % i == 0) return i;
return -1;
}
public static long max3(long a, long b, long c) {
return max2(max2(a, b), c);
}
public static long max2(long a, long b) {
return Math.max(a, b);
}
public static long min3(long a, long b, long c) {
return min2(min2(a, b), c);
}
public static long min2(long a, long b) {
return Math.min(a, b);
}
public static int max3(int a, int b, int c) {
return max2(max2(a, b), c);
}
public static int max2(int a, int b) {
return Math.max(a, b);
}
public static int min3(int a, int b, int c) {
return min2(min2(a, b), c);
}
public static int min2(int a, int b) {
return Math.min(a, b);
}
/**************************************HELPER FUNCTION ENDS HERE ************************************/
/*****************************************FAST INPUT STARTS HERE *************************************/
public static void FIO() {
sc = new Scanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
}
public static void closeIO() {
out.flush();
out.close();
}
private static class Scanner {
private final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer st = new StringTokenizer("");
public String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] set_int_array(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = nextInt();
return arr;
}
public long[] set_long_array(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) arr[i] = nextLong();
return arr;
}
public double[] set_double_array(int n) {
double[] arr = new double[n];
for (int i = 0; i < n; i++) arr[i] = sc.nextDouble();
return arr;
}
public int[][] set_2D_int_array(int n) {
return set_2D_int_array(n, n);
}
public int[][] set_2D_int_array(int row, int col) {
int[][] arr = new int[row][col];
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
arr[i][j] = nextInt();
return arr;
}
public long[][] set_2D_long_array(int n) {
return set_2D_long_array(n, n);
}
public long[][] set_2D_long_array(int row, int col) {
long[][] arr = new long[row][col];
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
arr[i][j] = nextLong();
return arr;
}
public char[][] set_2D_char_array(int n) {
return set_2D_char_array(n, n);
}
public char[][] set_2D_char_array(int row, int col) {
char[][] ch = new char[row][col];
for (int i = 0; i < row; i++)
ch[i] = sc.next().toCharArray();
return ch;
}
}
/*****************************************FAST INPUT ENDS HERE *************************************/
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 827eb0285625fe99d78ff8794e8fe7ca | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Solution {
/* -------------------------------------- Main Start ----------------------------------- */
public static void main(String[] args) throws IOException {
FastReader fs = new FastReader();
int t = Integer.parseInt(fs.nextLine());
while (t-- > 0) {
int n = fs.nextInt();
ArrayList<Long> arr = new ArrayList<>();
for (int j = 0; j < n; j++) {
arr.add(fs.nextLong());
}
Collections.sort(arr);
long cost = 0;
long beforeSum = 0;
long beforeCount = 0;
for (int i = 1; i < n; i++) {
cost += (arr.get(i) - arr.get(i - 1));
beforeCount++;
beforeSum += (arr.get(i - 1));
cost -= beforeCount * arr.get(i) - beforeSum;
}
System.out.println(cost);
}
}
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------
StringBuilder sb = new StringBuilder();
sb.append(x + "\n");
Collections.sort(arr, (a, b) -> Integer.compare(a[0], b[0]))
arr.toArray(new int[arr.size()][])
*/
private static final int MOD_1 = 1000000000 + 7;
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
String nextLine() throws IOException {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
/* --------------------------------------------------------------------------------------------------------------------------------------------------------------------- */
void printArray(int[] arr) {
System.out.println(Arrays.toString(arr));
}
void printArray(String[] arr) {
System.out.println(Arrays.toString(arr));
}
void printArray(long[] arr) {
System.out.println(Arrays.toString(arr));
}
void print(int data) {
System.out.println(data);
}
void print(String data) {
System.out.println(data);
}
void print(long data) {
System.out.println(data);
}
int[] II(int n) throws IOException {
int[] d = new int[n];
String[] arr = nextLine().split(" ");
for (int i = 0; i < n; i++) {
d[i] = Integer.parseInt(arr[i]);
}
return d;
}
String[] IS(int n) throws IOException {
return nextLine().split(" ");
}
long[] IL(int n) throws IOException {
long[] d = new long[n];
String[] arr = nextLine().split(" ");
for (int i = 0; i < n; i++) {
d[i] = Long.parseLong(arr[i]);
}
return d;
}
public long gcd(long a, long b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
public long power(long x, long y, long p) {
long res = 1;
x = x % p;
if (x == 0) {
return 0;
}
while (y > 0) {
if ((y & 1) == 1) {
res = (res * x) % p;
}
y = y >> 1;
x = (x * x) % p;
}
return res;
}
void sieveOfEratosthenes(boolean prime[], int size) {
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
prime[2] = true;
for (int p = 2; p * p < size; p++) {
if (prime[p] == true) {
for (int i = p * p; i < size; i += p) {
prime[i] = false;
}
}
}
}
public long fact(long n) {
long ans = 1;
for (int i = 2; i <= n; i++) {
ans = (ans * i) % MOD_1;
}
return ans;
}
public long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
// find first element in the arr that is equal to the x.
// If x is not present in arr then it will return first element that is greater than x
// It return arr.size() if all element are smaller than x
public int lowerBound(int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
// find first element in the arr that is greater than x.
// It return arr.size() if all element are smaller than x
public int upperBound(int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > x) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 08668dd7c5e408957f7f42f60236fddf | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | //package codechef;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
public class cfA {
static int countSetBits(int n)
{
int cnt=0;
while(n>0)
{
n=n&(n-1);
cnt++;
}
return cnt;
}
private static void shuffleArray(int[] array)
{
int index, temp;
Random random = new Random();
for (int i = array.length - 1; i > 0; i--)
{
index = random.nextInt(i + 1);
temp = array[index];
array[index] = array[i];
array[i] = temp;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
long arr[]=new long[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextLong();
}
//shuffleArray(arr);
Arrays.sort(arr);
long pre[]=new long[n];
pre[0]=arr[0];
for(int i=1;i<n;i++)
{
pre[i]=pre[i-1]+arr[i];
}
if(n<=2)
{
System.out.println(0);
continue;
}
long ans=0;
long mul=1;
for(int i=2;i<n;i++)
{
ans+=(arr[i]*mul)-pre[i-2];
mul++;
}
ans*=-(long)1;
System.out.println(ans);
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 60afc11fdb577e071de6260243a8ea9d | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main {
static int mod = 1000000007;
//--------------------------------- Scanner --------------------------------------//
public static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// -------------------------- Print and Flush ------------------------------------ //
public static void print(int[] arr) {
for(int i=0 ; i<arr.length ; i++) System.out.print(arr[i]+" ");
System.out.println();
}
public static void print(int[][] arr) {
for(int i=0 ; i<arr.length ; i++) {
for(int j=0 ; j<arr[0].length ; j++) System.out.print(arr[i][j]+" ");
System.out.println();
}
}
public static void print(int res) {
System.out.println(res);
}
public static void print(String res) {
System.out.println(res);
}
public static void print(int a , int b) {
System.out.println(a + " " + b);
}
public static void print(char[] res) {
for (int i = 0 ; i < res.length ; i++)
System.out.print(res[i]);
System.out.println();
}
public static void flush() {
System.out.flush();
}
// --------------------------- Is Palindrome ------------------------------------- //
public static boolean isPalindrome(String str) {
for (int i = 0 ; i < str.length() ; i++) {
if (str.charAt(i) != str.charAt(str.length() - i - 1))
return false;
}
return true;
}
//-------------------------------- Pair ------------------------------------------ //
public static class Pair {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
Pair(Pair p) {
x = p.x;
y = p.y;
}
}
//------------------------------- Comparator ------------------------------------ //
static class cmp implements Comparator<Pair> {
public int compare(Pair p1 , Pair p2) {
if (p1.y > p2.y)
return 1;
return -1;
}
}
// ----------------------------- Tries ------------------------------------------- //
static class Tries {
char ch = '\u0000';
int count = 0;
Tries[] arr = new Tries[26];
boolean isEnd = false;
Tries(char ch) {
this.ch = ch;
}
Tries () { }
}
static Tries dummy = new Tries();
static int tries_insert(String str) {
Tries temp = dummy;
for (int i = 0 ; i < str.length() ; i++) {
int pos = str.charAt(i) - 'a';
if (temp.arr[pos] == null)
temp.arr[pos] = new Tries(str.charAt(i));
temp = temp.arr[pos];
}
temp.isEnd = true;
temp.count += 1;
return temp.count;
}
// ------------------------- Prime Number (seive) ------------------------------- //
static int len = 10001;
static int[] prime_number = new int[len];
static Set<Integer> prime_number_set = new HashSet<>();
static Set<Long> three_factor_set = new HashSet<>();
static void seive() {
prime_number[0] = 1;
prime_number[1] = 1;
for (int i = 2 ; i < len ; i++) {
if (prime_number[i] == 0) {
long val = i;
prime_number_set.add(i);
//three_factor_set.add(val * val);
for (int j = i + i ; j < len ; j+=i)
prime_number[j] = 1;
}
}
}
static long[] product = new long[200001];
static void multiply() {
product[0] = 1;
product[1] = 1;
for (int i = 2 ; i < 200001 ; i++)
product[i] = (product[i - 1] * i) % mod;
}
//--------------------------- perfect square ------------------------------------ //
static Set<Integer> perfect_square = new HashSet();
static void ps(int len) {
for (int i = 1 ; i <= len ; i++) {
perfect_square.add(i * i);
}
}
// ---------------------------------- Sqrt -------------------------------------- //
static long pow(long a , long b , long M) {
if (b == 0)
return 1;
long mid = pow(a , b/2 , M) % M;
if (b % 2 == 0)
return (mid * mid) % M;
return (((mid * mid) % M) * a) % M;
}
static long sumDigit(long num) {
long res = 0;
while (num > 0) {
res += num % 10;
num = num / 10;
}
return res;
}
static long fact(int len , long M) {
long res = 1;
for (int i = 1 ; i < len ; i++) {
res = (res * i) % M;
}
return res % M;
}
// ------------------------------- GCD ---------------------------------------- //
static long gcd(long a , long b) {
if (a > b)
gcd(b , a);
if (a == 0)
return b;
return gcd(b % a , a);
}
// ----------------------------- LOWER BOND ------------------------------------ //
static int upperBond(int[] arr , int target) {
int st = 0 , end = arr.length - 1 , mid = 0;
while (st < end) {
mid = st + ((end - st) >> 1);
if (arr[mid] < target)
st = mid + 1;
else
end = mid;
}
return arr[end];
}
static int lowerBond(int[] arr , int target) {
int st = 0 , end = arr.length - 1 , mid = 0;
while (st < end) {
mid = st + ((end - st) >> 1);
if (arr[mid] > target)
end = mid - 1;
else
st = mid;
}
return arr[st];
}
// -------------------------- Solve --------------------------------------------- //
public static void solve(Scanner s) {
int n = s.nextInt();
long[] arr = new long[n];
for (int i = 0 ; i < n ; i++)
arr[i] = s.nextLong();
long res = 0;
if (n == 1 || n == 2) {
System.out.println(0);
return ;
}
Arrays.sort(arr);
for (int i = 1 ; i < n ; i++)
res -= (arr[i] - arr[i - 1]) * i * (n - i);
System.out.println(res + arr[n - 1]);
}
public static void main(String[] args) {
Scanner s = new Scanner();
// PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
// multiply();
// seive();
// ps(1000000);
// prime_number_set.add(1);
int t = s.nextInt();
// s.nextLine();
while(t-->0)
solve(s);
// e(s);
// int n = s.nextInt(); // read input as integer
// long k = s.nextLong(); // read input as long
// double d = s.nextDouble(); // read input as double
// String str = s.next(); // read input as String
// String str2 = s.nextLine(); // read whole line as String
// long result = 3*k;
// out.println(result); // print via PrintWriter
// out.close();
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | f5e99887ba9c8a22feead1f38c67f5ac | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.util.*;
public class answer {
static Scanner s=new Scanner(System.in);
static long mod=1000000007;
public static void main(String[] args) {
int testcases=s.nextInt();
while(testcases-->0) {
int n=s.nextInt();
long[] arr=new long[n];
for(int i=0;i<n;i++) {
arr[i]=s.nextLong();
}
Arrays.sort(arr);
if(n==1) {
System.out.println(0);
continue;
}
if(n==2) {
System.out.println(0);
continue;
}
long ans=0;
long sum=arr[1];
for(int i=2;i<n;i++) {
ans+=((i-1)*(arr[i]-arr[i-1]))+sum;
sum=((i-1)*(arr[i]-arr[i-1]))+sum;
sum+=arr[i]-arr[i-1];
}
System.out.println(ans*(long)-1);
// for(int i=n-1;i>=0;i--) {
//
// int num=(int)(2*n)/arr[i][0];
//
//
//
//
//
// }
}
}
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
public static long pow(long a,long b){
long ans=1;
if(a==0)return 1;
for(int i=0;i<b;i++) {
ans*=a;
ans%=mod;
}
return ans;
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | f9217963e210c9d6d896f0395cf23d85 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.util.*;
public class answer {
static Scanner s=new Scanner(System.in);
static long mod=1000000007;
public static void main(String[] args) {
int testcases=s.nextInt();
while(testcases-->0) {
int n=s.nextInt();
long[] arr=new long[n];
for(int i = 0; i < n; i++) {
arr[i]=s.nextLong();
}
Arrays.sort(arr);
long[] dp=new long[n];
for(int i = 2; i < n; i++) {
dp[i] = dp[i - 1] + ((i - 2) * (arr[i] - arr[i - 1]));
dp[i] += (arr[i] - arr[i - 2]);
}
long ans = 0;
for(long i : dp)
ans -= i;
System.out.println(ans);
}
}
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
public static long pow(long a,long b){
long ans=1;
if(a==0)return 1;
for(int i=0;i<b;i++) {
ans*=a;
ans%=mod;
}
return ans;
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | f079d483298e689a8c0159b14dc51736 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
static long mod = (long) Math.pow(10, 9) + 7;
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
StringBuilder fout = new StringBuilder();
int t = sc.nextInt();
while (t-- != 0) {
int n = sc.nextInt();
long a[]=new long[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextLong();
}
Arrays.sort(a);
long val=0,sum=0;
for(int i=2;i<n;i++)
{
sum+=a[i-2];
val-=((long) a[i] *(i-1));
val+=sum;
}
System.out.println(val);
}
}
static void print(long[] arr) {
for (long l : arr) System.out.print(l + " ");
}
static void print(int[] arr) {
for (int j : arr) System.out.print(j + " ");
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static void no() {
System.out.println("NO");
}
static void yes() {
System.out.println("YES");
}
static class Pair {
int a, b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
}
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 6c2b08613138cdef8d819b4d83e0cf29 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
public class c{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for ( ; t > 0; t--){
int n = in.nextInt();
Integer[] vals = new Integer[n];
for (int i = 0; i < n; i++)
vals[i] = in.nextInt();
Arrays.sort(vals);
long sum = 0;
for (int i = 1; i < n; i++)
sum += (vals[i] - vals[i - 1]);
long[] pre = new long[n];
long neg = 0;
for (int i = 1; i < n; i++){
pre[i] = pre[i - 1] + (long)(vals[i] - vals[i - 1]) * i;
neg += pre[i];
}
System.out.println(sum - neg);
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 32f941472d1443b4c9449773b2004f1a | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class ok {
public static void main(String[] args) {
FastReader sc = new FastReader();
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
}
if (n == 1 || n == 2) {
System.out.println(0);
continue;
}
Arrays.sort(arr);
long ans = 0;
for (int j = 1; j < n - 1; j++) {
ans -= arr[j];
}
int m = n - 1;
int k = 0;
while (k < m) {
ans -= ((k - m + k + 1l) * arr[k + 1]);
k++;
}
System.out.println(ans);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long[] readArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | ba2744e62e8f6337ea78a61d61b2a423 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
import java.text.*;
public class Codeforces {
static int mod=1000_000_007 ;
public static void main(String[] args) throws Exception {
PrintWriter out=new PrintWriter(System.out);
FastScanner fs=new FastScanner();
int t=fs.nextInt();
outer :while(t-->0) {
int n=fs.nextInt();
long arr[]=new long[n];
for(int i=0;i<n;i++) arr[i]=fs.nextLong();
sort(arr);
long pre[]=new long[n];
pre[0]=arr[0];
for(int i=1;i<n;i++) {
pre[i]=pre[i-1]+arr[i];
}
long ans=0;
long mul=1;
for(int i=2;i<n;i++) {
ans -= ((arr[i]*mul) - pre[i-2]);
mul++;
}
// ans*=-1;
out.println(ans);
}
out.close();
}
static long fact(long n) {
long res=1;
for(int i=2;i<=n;i++) {
res*=i;
res%=mod;
}
return res;
}
static int gcd(int a,int b) {
if(b==0) return a;
return gcd(b,a%b);
}
static void sort(long[] a) {
//suffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n);
long temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
// Use this to input code since it is faster than a Scanner
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 630933cfec7945495d6113593d6a0300 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Main
{
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
static Reader sc=new Reader();
static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
static void sort(int[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static long fun(ArrayList<Integer> arr)
{
int n=arr.size();
long result = 0;
for (int i=0; i<n; i++)
result += (arr.get(i) *1l* (i+1) * (n-i));
return result ;
}
public static void main(String args[])throws IOException
{
/*
* For integer input: int n=inputInt();
* For long input: long n=inputLong();
* For double input: double n=inputDouble();
* For String input: String s=inputString();
* Logic goes here
* For printing without space: print(a+""); where a is a variable of any datatype
* For printing with space: printSp(a+""); where a is a variable of any datatype
* For printing with new line: println(a+""); where a is a variable of any datatype
*/
int t=inputInt();
while(t-->0){
int n=inputInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=inputInt();
}
sort(arr);
ArrayList<Integer> temp=new ArrayList<>();
for(int i=1;i<n;i++){
temp.add(arr[i]-arr[i-1]);
}
long ans=0;
for(long c:temp){
ans+=c;
}
ans-=fun(temp);
System.out.println(ans);
}
}
public static int inputInt()throws IOException
{
return sc.nextInt();
}
public static long inputLong()throws IOException
{
return sc.nextLong();
}
public static double inputDouble()throws IOException
{
return sc.nextDouble();
}
public static String inputString()throws IOException
{
return sc.readLine();
}
public static void print(String a)throws IOException
{
bw.write(a);
}
public static void printSp(String a)throws IOException
{
bw.write(a+" ");
}
public static void println(String a)throws IOException
{
bw.write(a+"\n");
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 9b1bce02f9e1fca71f2b58e7e1d67c8a | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | //package codeForces728;
//package codeForces728;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class B {
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static MyScanner sc = new MyScanner();
static int pi(String s) {
return Integer.parseInt(s);
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static final long rem = (long) (1e9 + 7);
static final long intmax = (long) (Integer.MAX_VALUE);
public static void main(String[] args) throws Exception {
int t = sc.nextInt();
for (int q = 0; q < t; q++) {
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
long sum = 0;
for (int i = 0; i < n; i++) {
sum -= (long) (n - 1 - i) * arr[i];
sum += (long) i * arr[i];
}
for (int i = 1; i < n; i++) {
sum -= ((long) arr[i] - arr[i - 1]);
}
out.println(-sum);
}
out.flush();
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | f8ab8c4879942b0259e0da5fe42bdca3 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
import java.io.*;
public class C_1541 {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int[] array = sc.shuffle(sc.nextIntArray(n));
Arrays.sort(array);
if(n < 3)
pw.println(0);
else {
int[] arr = new int[n - 1];
for(int i = 1; i < n; i++)
arr[i - 1] = array[i] - array[i - 1];
long sum = 0;
long[] mul = new long[n - 1];
for(int i = 0; i < n - 1; i++)
sum += 1l * (1l * (i + 1) * (n - 1 - i) - 1) * arr[i];
pw.println(-sum);
}
}
pw.flush();
}
public static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public int[] nextIntArray(int n) throws IOException {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] array = new Integer[n];
for (int i = 0; i < n; i++)
array[i] = new Integer(nextInt());
return array;
}
public long[] nextLongArray(int n) throws IOException {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nextLong();
return array;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nextDouble();
return array;
}
public static int[] shuffle(int[] a) {
int n = a.length;
Random rand = new Random();
for (int i = 0; i < n; i++) {
int tmpIdx = rand.nextInt(n);
int tmp = a[i];
a[i] = a[tmpIdx];
a[tmpIdx] = tmp;
}
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | d8d68818193fe5def9ffe7ea996ddd82 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main extends PrintWriter {
Main() { super(System.out); }
static boolean cases = true;
// Solution
void solve(int t) {
int n = sc.nextInt();
int a[] = sc.readIntArray(n);
if (n <= 2) {
println(0);
return;
}
Arrays.sort(a);
long sum = a[n - 1];
long prev = 0;
for (int i = 1; i < n; i++) {
prev += (long) i * (long) (a[i] - a[i - 1]);
sum -= prev;
}
println(sum);
}
public static void main(String[] args) {
Main obj = new Main();
int c = 1;
for (int t = (cases ? sc.nextInt() : 0); t > 1; t--, c++) obj.solve(c);
obj.solve(c);
obj.flush();
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
int[] readIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
char[] readCharArray(int n) {
char a[] = new char[n];
String s = sc.next();
for (int i = 0; i < n; i++) { a[i] = s.charAt(i); }
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() { return Long.parseLong(next()); }
}
private static final FastScanner sc = new FastScanner();
private PrintWriter out = new PrintWriter(System.out);
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | caad3f2cf2f2cd3737ec97935b3da1c8 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) throws Exception {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream(java.io.FileDescriptor.out), StandardCharsets.US_ASCII), 512);
FastReader sc=new FastReader();
int t=sc.nextInt();
while (t-->0){
int n = sc.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
if(n<=2){
out.write(0+"\n");
out.flush();
continue;
}
Arrays.sort(arr);
long ans=0;
long fans=0;
for(int i=0;i<n-1;i++){
ans+=arr[i];
// out.write(ans+" ");
fans+=(ans-((long)arr[i+1]*(long) (i+1)));
// out.write(fans+" ");
}
fans+=arr[n-1];
out.write(fans+"\n");
out.flush();
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | ff8bfa4519637bf4ca519aa47cacd000 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.PrintWriter;
import java.util.*;
public class Practice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
long[] a = new long[n];
for (int i=0;i<n;i++){
a[i] = sc.nextInt();
}
Arrays.sort(a);
long ans = n>=3 ? -a[2] : 0;
long prevVal = n>=3 ? a[2] : 0;
for(int i=3;i<n;i++){
long num = (i-2);
long diff = a[i] - a[i-1];
prevVal += num*(diff);
prevVal += a[i] - a[i-2];
// System.out.println(prevVal);
ans -= prevVal;
}
System.out.println(ans);
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 9bde7509a7c2d2c87f94cb13b867fc40 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
l:
while(t-- > 0) {
int n = sc.nextInt();
int[] a = new int[n];
long[] pre = new long[n];
for(int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
Arrays.sort(a);
pre[0] = a[0];
for(int i = 1; i < n; i++) {
pre[i] = 0l + pre[i-1] + a[i];
}
long ans = 0l;
for(int i = 0; i < n; i++) {
ans += (i+1)*1l*(a[i]) - pre[i];
}
ans -= a[n-1];
System.out.println(-ans);
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | e0dda4adecce25e9439d79c132494e81 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.util.*;
import java.io.*;
public class C {
static StringBuilder sb;
static dsu dsu;
static long fact[];
static int mod = (int) (1e9 + 7);
static void solve() {
long n = l();
long[] arr = readArray(n);
arr = sortlong(arr);
long ans = arr[arr.length - 1];
long[] neg = new long[(int) n];
for (int i = 1; i < arr.length; i++) {
neg[i] = (neg[i - 1] + ((arr[i] - arr[i - 1]) * i));
ans -= neg[i];
}
sb.append(ans + "\n");
}
public static void main(String[] args) {
sb = new StringBuilder();
int test = i();
while (test-- > 0) {
solve();
}
System.out.println(sb);
}
/*
* fact=new long[(int)1e6+10]; fact[0]=fact[1]=1; for(int i=2;i<fact.length;i++)
* { fact[i]=((long)(i%mod)1L(long)(fact[i-1]%mod))%mod; }
*/
//**************NCR%P******************
static long ncr(int n, int r) {
if (r > n)
return (long) 0;
long res = fact[n] % mod;
// System.out.println(res);
res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod;
res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod;
// System.out.println(res);
return res;
}
static long p(long x, long y)// POWER FXN //
{
if (y == 0)
return 1;
long res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = (res * x) % mod;
y--;
}
x = (x * x) % mod;
y = y / 2;
}
return res;
}
//**************END******************
// *************Disjoint set
// union*********//
static class dsu {
int parent[];
dsu(int n) {
parent = new int[n];
for (int i = 0; i < n; i++)
parent[i] = -1;
}
int find(int a) {
if (parent[a] < 0)
return a;
else {
int x = find(parent[a]);
parent[a] = x;
return x;
}
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
parent[b] = a;
}
}
//**************PRIME FACTORIZE **********************************//
static TreeMap<Integer, Integer> prime(long n) {
TreeMap<Integer, Integer> h = new TreeMap<>();
long num = n;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (n % i == 0) {
int nt = 0;
while (n % i == 0) {
n = n / i;
nt++;
}
h.put(i, nt);
}
}
if (n != 1)
h.put((int) n, 1);
return h;
}
//****CLASS PAIR ************************************************
static class Pair implements Comparable<Pair> {
int x;
long y;
Pair(int x, long y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
return (int) (this.y - o.y);
}
}
//****CLASS PAIR **************************************************
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static InputReader in = new InputReader(System.in);
static OutputWriter out = new OutputWriter(System.out);
public static long[] sortlong(long[] a2) {
int n = a2.length;
ArrayList<Long> l = new ArrayList<>();
for (long i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static int[] sortint(int[] a2) {
int n = a2.length;
ArrayList<Integer> l = new ArrayList<>();
for (int i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static long pow(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 != 0) {
res = (res * x);// % modulus;
y--;
}
x = (x * x);// % modulus;
y = y / 2;
}
return res;
}
//GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
// ******LOWEST COMMON MULTIPLE
// *********************************************
public static long lcm(long x, long y) {
return (x * (y / gcd(x, y)));
}
//INPUT PATTERN********************************************************
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
public static int[] readArray(int n) {
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = i();
}
return A;
}
public static long[] readArray(long n) {
long A[] = new long[(int) n];
for (int i = 0; i < n; i++) {
A[i] = l();
}
return A;
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 62e3729da928a3732d85a571992d6e44 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author Acer
*/
public class GreatGraphs_C {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T-- > 0){
int n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
int sum = arr[n-1];
long neg = 0;
long sumNeg = 0;
for (int i = 1; i < n; i++) {
long diff = arr[i] - arr[i-1];
neg = (i*diff) + neg;
sumNeg+=neg;
}
sumNeg = sumNeg*(-1);
System.out.println(sum+sumNeg);
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | d9a1ace4096c52a2af665514e0ae3181 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.stream.Collectors;
public class Main {
static int mod = (int) (1e9) + 7;
/* ======================DSU===================== */
static class dsu {
static int parent[], n;// min[],value[];
static long size[];
dsu(int n) {
parent = new int[n + 1];
size = new long[n + 1];
// min=new int[n+1];
// value=new int[n+1];
this.n = n;
makeSet();
}
static void makeSet() {
for (int i = 1; i <= n; i++) {
parent[i] = i;
size[i] = 1;
// min[i]=i;
}
}
static int find(int a) {
if (parent[a] == a)
return a;
else {
return parent[a] = find(parent[a]);// Path Compression
}
}
static void union(int a, int b) {
int setA = find(a);
int setB = find(b);
if (setA == setB)
return;
if (size[setA] >= size[setB]) {
parent[setB] = setA;
size[setA] += size[setB];
} else {
parent[setA] = setB;
size[setB] += size[setA];
}
}
}
/* ======================================================== */
static class Pair<X extends Number, Y extends Number> implements Comparator<Pair> {
X x;
Y y;
// Constructor
public Pair(X x, Y y) {
this.x = x;
this.y = y;
}
public Pair() {
}
@Override
public int compare(Main.Pair o1, Main.Pair o2) {
return ((int) (o1.y.intValue() - o2.y.intValue()));// Ascending Order based on 'y'
}
}
/* ===============================Tries================================= */
static class TrieNode {
private HashMap<Character, TrieNode> children = new HashMap<>();
public int size;
boolean endOfWord;
public void putChildIfAbsent(char ch) {
children.putIfAbsent(ch, new TrieNode());
}
public TrieNode getChild(char ch) {
return children.get(ch);
}
}
static private TrieNode root;
public static void insert(String str) {
TrieNode curr = root;
for (char ch : str.toCharArray()) {
curr.putChildIfAbsent(ch);
curr = curr.getChild(ch);
curr.size++;
}
// mark the current nodes endOfWord as true
curr.endOfWord = true;
}
public static int search(String word) {
TrieNode curr = root;
for (char ch : word.toCharArray()) {
curr = curr.getChild(ch);
if (curr == null) {
return 0;
}
}
// size contains words starting with prefix- word
return curr.size;
}
public boolean delete(TrieNode current, String word, int index) {
if (index == word.length()) {
// when end of word is reached only delete if currrent.endOfWord is true.
if (!current.endOfWord) {
return false;
}
current.endOfWord = false;
// if current has no other mapping then return true
return current.children.size() == 0;
}
char ch = word.charAt(index);
TrieNode node = current.children.get(ch);
if (node == null) {
return false;
}
boolean shouldDeleteCurrentNode = delete(node, word, index + 1);
// if true is returned then delete the mapping of character and trienode
// reference from map.
if (shouldDeleteCurrentNode) {
current.children.remove(ch);
// return true if no mappings are left in the map.
return current.children.size() == 0;
}
return false;
}
/* ================================================================= */
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] intArr(int n) {
int res[] = new int[n];
for (int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
long[] longArr(int n) {
long res[] = new long[n];
for (int i = 0; i < n; i++)
res[i] = nextLong();
return res;
}
}
static FastReader f = new FastReader();
static BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
static boolean isPrime(long n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int lowerBound(int a[], int x) { // x is the target, returns lowerBound. If not found return -1
int l = -1, r = a.length, flag = 0;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (x == a[m])
flag = 1;
if (a[m] >= x)
r = m;
else
l = m;
}
return r;
}
static int upperBound(int a[], int x) {// x is the target, returns upperBound. If not found return -1
int l = -1, r = a.length, flag = 0;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] == x)
flag = 1;
if (a[m] <= x)
l = m;
else
r = m;
}
return (l + 1);
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
static long power(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y >>= 1;
x = (x * x);
}
return res;
}
public double binPow(double x, int n) {// binary exponentiation with negative power as well
if (n == 0)
return 1.0;
double binPow = binPow(x, n / 2);
if (n % 2 == 0) {
return binPow * binPow;
} else {
return n > 0 ? (binPow * binPow * x) : (binPow * binPow / x);
}
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
/*
* ===========Modular Operations==================
*/
static long modPower(long x, long y, long p) {
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p) {
return modPower(n, p - 2, p);
}
static long modAdd(long a, long b) {
return ((a + b + mod) % mod);
}
static long modMul(long a, long b) {
return ((a % mod) * (b % mod)) % mod;
}
static long nCrModPFermat(int n, int r) {
if (r == 0)
return 1;
long[] fac = new long[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % mod;
return (fac[n] * modInverse(fac[r], mod) % mod * modInverse(fac[n - r], mod) % mod) % mod;
}
/*
* ===============================================
*/
static void ruffleSort(long[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n);
long temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(int[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n);
int temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
static String binString32(int n) {
StringBuilder str = new StringBuilder("");
String bin = Integer.toBinaryString(n);
if (bin.length() != 32) {
for (int k = 0; k < 32 - bin.length(); k++) {
str.append("0");
}
str.append(bin);
}
return str.toString();
}
static class sparseTable {
public static int st[][];
public static int log = 4;
static int func(int a, int b) {// make func as per question(here min range query)
return (int) gcd(a, b);
}
void makeTable(int n, int a[]) {
st = new int[n][log];
for (int i = 0; i < n; i++) {
st[i][0] = a[i];
}
for (int j = 1; j < log; j++) {
for (int i = 0; i + (1 << j) <= n; i++) {
st[i][j] = func(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
}
}
static int query(int l, int r) {
int length = r - l + 1;
int k = 0;
while ((1 << (k + 1)) <= length) {
k++;
}
return func(st[l][k], st[r - (1 << k) + 1][k]);
}
static void printTable(int n) {
for (int j = 0; j < log; j++) {
for (int i = 0; i < n; i++) {
System.out.print(st[i][j] + " ");
}
System.out.println();
}
}
}
/*
* ====================================Main=================================
*/
// static int st[], a[];
// static void buildTree(int treeIndex, int lo, int hi) {
// if (hi == lo) {
// st[treeIndex] = a[lo];
// return;
// }
// int mid = (lo + hi) / 2;
// buildTree(treeIndex * 2 + 1, lo, mid);
// buildTree(treeIndex * 2 + 2, mid + 1, hi);
// st[treeIndex] = merge(st[treeIndex * 2 + 1], st[treeIndex * 2 + 2]);
// }
// static void update(int treeIndex, int lo, int hi, int arrIndex, int val) {
// if (hi == lo) {
// st[treeIndex] = val;
// a[arrIndex] = val;
// return;
// }
// int mid = (hi + lo) / 2;
// if (mid < arrIndex) {
// update(treeIndex * 2 + 2, mid + 1, hi, arrIndex, val);
// } else {
// update(treeIndex * 2 + 1, lo, mid, arrIndex, val);
// }
// st[treeIndex] = merge(st[treeIndex * 2 + 1], st[treeIndex * 2 + 2]);
// }
// static int query(int treeIndex, int lo, int hi, int l, int r) {
// if (l <= lo && r >= hi) {
// return st[treeIndex];
// }
// if (l > hi || r < lo) {
// return 0;
// }
// int mid = (hi + lo) / 2;
// return query(treeIndex * 2 + 1, lo, mid, l, Math.min(mid, r));
// }
// static int merge(int a, int b) {
// return a + b;
// }
static long dp[][];
public static long findKthPositive(long[] A, long k) {
int l = 0, r = A.length, m;
while (l < r) {
m = (l + r) / 2;
if (A[m] - 1 - m < k)
l = m + 1;
else
r = m;
}
return l + k;
}
static int[] z_function(char ar[]) {
int[] z = new int[ar.length];
z[0] = ar.length;
int l = 0;
int r = 0;
for (int i = 1; i < ar.length; i++) {
if (r < i) {
l = i;
r = i;
while (r < ar.length && ar[r - l] == ar[r])
r++;
z[i] = r - l;
r--;
} else {
int k = i - l;
if (z[k] < r - i + 1) {
z[i] = z[k];
} else {
l = i;
while (r < ar.length && ar[r - l] == ar[r])
r++;
z[i] = r - l;
r--;
}
}
}
return z;
}
public static void main(String args[]) throws Exception {
// File file = new File("D:\\VS Code\\Java\\Output.txt");
// FileWriter fw = new FileWriter("D:\\VS Code\\Java\\Output.txt");
int t = 1;
t = f.nextInt();
int tc = 1;
while (t-- != 0) {
// BufferedReader br = new BufferedReader(new FileReader("PHOBIA.INP"));
// PrintWriter pw = new PrintWriter(new BufferedWriter(new
// FileWriter("PHOBIA.OUT")));
int n=f.nextInt();
int d[]=new int[n];
// long sum=0;
for(int i=0;i<n;i++){
d[i]=f.nextInt();
// sum+=(long)d[i];
}
if(n==1 || n==2){
w.write(0+"\n");
}else{
ruffleSort(d);
long sum=0;
int diff[]=new int[n-1];
for(int i=1;i<n;i++){
diff[i-1]=(d[i]-d[i-1]);
// System.out.print(diff[i-1]+" ");
sum+=diff[i-1];
}
// System.out.println();
w.write(sum-func(diff)+"\n");
}
}
w.flush();
}
static long func(int d[]){
long sum=0;
int n=d.length;
for(int i=0;i<n;i++){
sum+=d[i]*1l*(n-i)*(i+1);
}
return sum;
}
}
/*
*/ | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 178ff0203dab00f8942f99059014ae12 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class GreatGraphs {
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));//new FileReader("cowdance.in")
PrintWriter out = new PrintWriter(System.out);//new FileWriter("cowdance.out")
StringTokenizer st = new StringTokenizer(read.readLine());
int t = Integer.parseInt(st.nextToken());
for (int ie = 0; ie < t; ie++) {
st = new StringTokenizer(read.readLine());
int n = Integer.parseInt(st.nextToken());
long [] a = new long [n];
st = new StringTokenizer(read.readLine());
for (int i = 0; i < n; i++) {
a[i] = Long.parseLong(st.nextToken());
}
Arrays.sort(a);
long ans = 0;
long sum = 0;
for (int i = 2; i < n; i++) {
sum += a[i-2];
ans -= (a[i]*(i-1));
ans += sum;
}
out.println(ans);
}
out.close();
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 4a41f21e373e7283eebd866247592b6f | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
public class c {
static BufferedWriter op = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String args[]) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int q=Integer.parseInt(br.readLine().trim());
for (int iq=1;iq<=q;iq++){
String xi[]=br.readLine().trim().split(" ");
int n=Integer.parseInt(xi[0]);
xi=br.readLine().trim().split(" ");
long ar[]= new long[n];
for (int i=0;i<n;i++)
ar[i]=Integer.parseInt(xi[i]);
Arrays.sort(ar);
long val=0,sum=0;
for (int i=2;i<n;i++){
sum+=ar[i-2];
val=val-(ar[i]*(i-1))+sum;
}
op.write( val +" \n");
}
op.flush();
op.close();
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 2f0f16bee5f6c1d7ed510e1289eea1bc | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Main implements Runnable {
int n, m, k;
static boolean use_n_tests = true;
long mod = 1_000_000_007;
void solve(FastScanner in, PrintWriter out, int testNumber) {
n = in.nextInt();
Integer[] d = in.nextArray2(n);
Arrays.sort(d);
if (n <= 2) {
out.println(0);
} else {
long ans = 0;
long total = 0;
for (int i = 2; i < n; i++) {
total += d[i - 2];
ans += d[i] * 1l * (i - 1);
ans -= total;
}
out.println(-ans);
}
}
class Pur {
Pur(long need, long dicc) {
this.need = need;
this.disc = dicc;
diff = Math.max(0, dicc - need);
}
long getDisc() {
return disc;
}
long need;
long disc;
long diff;
}
// ****************************** template code ***********
void swap(Integer[] a, int i, int j) {
Integer tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
long manhDist(long x, long y, long x1, long y1) {
return Math.abs(x - x1) + Math.abs(y - y1);
}
double dist(double x, double y, double x1, double y1) {
return Math.sqrt(Math.pow(x - x1, 2.0) + Math.pow(y - y1, 2.0));
}
public static class FW {
public static void add(long[] t, int i, long value) {
for (; i < t.length; i |= i + 1)
t[i] += value;
}
public static long sum(long[] t, int i) {
long res = 0;
for (; i >= 0; i = (i & (i + 1)) - 1)
res += t[i];
return res;
}
public static void add(long[] t, int a, int b, long value) {
add(t, a, value);
add(t, b + 1, -value);
}
}
int sign(int a) {
if (a < 0) {
return -1;
}
return 1;
}
long binpow(long a, int b) {
long res = 1;
while (b != 0) {
if (b % 2 == 0) {
b /= 2;
a *= a;
a %= mod;
}
b--;
res *= a;
res %= mod;
}
return res;
}
List<Integer> getGidits(long n) {
List<Integer> res = new ArrayList<>();
while (n != 0) {
res.add((int) (n % 10L));
n /= 10;
}
return res;
}
List<Integer> generatePrimes(int n) {
List<Integer> res = new ArrayList<>();
boolean[] sieve = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
if (!sieve[i]) {
res.add(i);
}
if ((long) i * i <= n) {
for (int j = i * i; j <= n; j += i) {
sieve[j] = true;
}
}
}
return res;
}
int[] ask(int l) {
System.out.printf("? %d\n", l);
System.out.flush();
return in.nextArray(n);
}
static int stack_size = 1 << 27;
static class Coeff {
long mod;
long[][] C;
long[] fact;
boolean cycleWay = false;
Coeff(int n, long mod) {
this.mod = mod;
fact = new long[n + 1];
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = i;
fact[i] %= mod;
fact[i] *= fact[i - 1];
fact[i] %= mod;
}
}
Coeff(int n, int m, long mod) {
// n > m
cycleWay = true;
this.mod = mod;
C = new long[n + 1][m + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= Math.min(i, m); j++) {
if (j == 0 || j == i) {
C[i][j] = 1;
} else {
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
C[i][j] %= mod;
}
}
}
}
public long C(int n, int m) {
if (cycleWay) {
return C[n][m];
}
return fC(n, m);
}
private long fC(int n, int m) {
return (fact[n] * inv(fact[n - m] * fact[m] % mod)) % mod;
}
private long inv(long r) {
if (r == 1)
return 1;
return ((mod - mod / r) * inv(mod % r)) % mod;
}
}
class Pair {
int first;
long second;
Pair(int f, long s) {
first = f;
second = s;
}
public int getFirst() {
return first;
}
public long getSecond() {
return second;
}
}
class MultisetTree<T> {
int size = 0;
TreeMap<T, Integer> mp = new TreeMap<>();
void add(T x) {
mp.merge(x, 1, Integer::sum);
size++;
}
void remove(T x) {
if (mp.containsKey(x)) {
mp.merge(x, -1, Integer::sum);
if (mp.get(x) == 0) {
mp.remove(x);
}
size--;
}
}
boolean contains(T x) {
return mp.containsKey(x);
}
T greatest() {
return mp.lastKey();
}
T smallest() {
return mp.firstKey();
}
int size() {
return size;
}
int diffSize() {
return mp.size();
}
}
class Multiset<T> {
int size = 0;
Map<T, Integer> mp = new HashMap<>();
void add(T x) {
mp.merge(x, 1, Integer::sum);
size++;
}
boolean contains(T x) {
return mp.containsKey(x);
}
void remove(T x) {
if (mp.containsKey(x)) {
mp.merge(x, -1, Integer::sum);
if (mp.get(x) == 0) {
mp.remove(x);
}
size--;
}
}
int size() {
return size;
}
int diffSize() {
return mp.size();
}
}
static class Range {
int l, r;
int id;
public int getL() {
return l;
}
public int getR() {
return r;
}
public Range(int l, int r, int id) {
this.l = l;
this.r = r;
this.id = id;
}
}
static class Array {
static Range[] readRanges(int n, FastScanner in) {
Range[] result = new Range[n];
for (int i = 0; i < n; i++) {
result[i] = new Range(in.nextInt(), in.nextInt(), i);
}
return result;
}
static List<List<Integer>> intInit2D(int n) {
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
res.add(new ArrayList<>());
}
return res;
}
static boolean isSorted(Integer[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
static public long sum(int[] a) {
long sum = 0;
for (int x : a) {
sum += x;
}
return sum;
}
static public long sum(long[] a) {
long sum = 0;
for (long x : a) {
sum += x;
}
return sum;
}
static public long sum(Integer[] a) {
long sum = 0;
for (int x : a) {
sum += x;
}
return sum;
}
static public int min(Integer[] a) {
int mn = Integer.MAX_VALUE;
for (int x : a) {
mn = Math.min(mn, x);
}
return mn;
}
static public int min(int[] a) {
int mn = Integer.MAX_VALUE;
for (int x : a) {
mn = Math.min(mn, x);
}
return mn;
}
static public int max(Integer[] a) {
int mx = Integer.MIN_VALUE;
for (int x : a) {
mx = Math.max(mx, x);
}
return mx;
}
static public int max(int[] a) {
int mx = Integer.MIN_VALUE;
for (int x : a) {
mx = Math.max(mx, x);
}
return mx;
}
static public int[] readint(int n, FastScanner in) {
int[] out = new int[n];
for (int i = 0; i < out.length; i++) {
out[i] = in.nextInt();
}
return out;
}
}
class Graph {
List<List<Integer>> graph;
Graph(int n) {
create(n);
}
private void create(int n) {
List<List<Integer>> graph = new ArrayList<>();
for (int i = 0; i < n; i++) {
graph.add(new ArrayList<>());
}
this.graph = graph;
}
void read(int m, FastScanner in) {
for (int i = 0; i < m; i++) {
int v = in.nextInt() - 1;
int u = in.nextInt() - 1;
graph.get(v).add(u);
}
}
void readBi(int m, FastScanner in) {
for (int i = 0; i < m; i++) {
int v = in.nextInt() - 1;
int u = in.nextInt() - 1;
graph.get(v).add(u);
graph.get(u).add(v);
}
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream io) {
br = new BufferedReader(new InputStreamReader(io));
}
public String line() {
String result = "";
try {
result = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public char[] nextc() {
return next().toCharArray();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextInt();
}
return res;
}
public long[] nextArrayL(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextLong();
}
return res;
}
public Long[] nextArrayL2(int n) {
Long[] res = new Long[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextLong();
}
return res;
}
public Integer[] nextArray2(int n) {
Integer[] res = new Integer[n];
for (int i = 0; i < n; i++) {
res[i] = in.nextInt();
}
return res;
}
public long nextLong() {
return Long.parseLong(next());
}
}
void run_t_tests() {
int t = in.nextInt();
int i = 0;
while (t-- > 0) {
solve(in, out, i++);
}
}
void run_one() {
solve(in, out, -1);
}
@Override
public void run() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
if (use_n_tests) {
run_t_tests();
} else {
run_one();
}
out.close();
}
static FastScanner in;
static PrintWriter out;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(null, new Main(), "", stack_size);
thread.start();
thread.join();
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | c9017500ba3463741f4e55d6d3051fd3 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
public class Solution{
static FastScanner fs=new FastScanner();
static void output(){
int n = fs.nextInt();
long[] a = new long[n];
for(int i=0;i<n;i++){
a[i] = fs.nextLong();
}
long ps=0,ns=0;
Arrays.sort(a);
for(int i=2;i<n;i++){
ps += a[i-2];
ns -= (i-1)*a[i];
ns += ps;
}
System.out.println(ns);
}
public static void main(String[] args) {
int T = 1;
T=fs.nextInt();
for (int tt=0; tt<T; tt++) {
output();
}
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | e9d9ea95851712923cea9806feb36070 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
public class CodeForce_GreatGraphs {
public static void main(String[] args) {
int t = ni();
StringBuilder outBuffer = new StringBuilder();
while (t-- > 0) {
int n = ni();
List<Long> time = new ArrayList<>();
for (int i = 0; i < n; i++) {
time.add(nl());
}
time.sort((t1, t2) -> Long.compare(t1, t2));
long res = 0, sum = 0;
for (int i = 2; i < n; i++) {
sum += time.get(i - 2);
res -= (time.get(i) * (i - 1));
res += sum;
}
outBuffer.append(res + "\n");
}
System.out.println(outBuffer);
}
static InputStream is = System.in;
static byte[] inbuf = new byte[1 << 24];
static int lenbuf = 0, ptrbuf = 0;
static int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
static int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
static double nd() {
return Double.parseDouble(ns());
}
static char nc() {
return (char) skip();
}
static String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
static char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
static int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
static long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 8aad099cd5d7dfdefcd8108415c8410d | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
// * * * the goal is to be worlds best * * * //
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class C {
static class Pair implements Comparable<Pair>{
int a;
int b;
Pair(int a , int b){
this.a = a;
this.b = b;
}
public int compareTo(Pair o){
return this.a - o.a;
}
}
//==================================================================================================
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
long d[] = sc.readLongArray(n);
Arrays.sort(d);
long sum = 0;
long res = 0;
long pre[] = new long[n];
pre[0] = 0;
for(int i = 2; i < n; i++){
sum += d[i - 2];
pre[i] = sum;
}
for(int i = 2; i < n; i++){
res += pre[i];
}
for(int i = 2; i < n; i++){
res -= d[i] * (i - 1);
}
System.out.println(res);
}
}
//==================================================================================================
// // Use this instead of Arrays.sort() on an array of ints. Arrays.sort() is n^2
// // worst case since it uses a version of quicksort. Although this would never
// // actually show up in the real world, in codeforces, people can hack, so
// // this is needed.
// static void sort(long[] d) {
// //ruffle
// long n=d.length;
// Random r=new Random();
// for (int i=0; i<d.length; i++) {
// long oi=r.nextint(n), temp=d[i];
// d[i]=d[oi];
// d[oi]=temp;
// }
// //then sort
// Arrays.sort(d);
// }
// Use this to input code since it is faster than a Scanner
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String str = "";
String nextLine() {
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
// generates all the prime numbers upto n
static void sieveOfEratosthenes(int n , ArrayList<Integer> al)
{
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
al.add(i);
}
}
static final int M = 1000_000_000 + 7;
static final int imx = Integer.MAX_VALUE;
static final int imi = Integer.MIN_VALUE;
//fastPow
static long fastPow(long base, long exp) {
if (exp==0) return 1;
long half=fastPow(base, exp/2);
if (exp%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
// multiply two long numbers
static long mul(long a, long b) {
return a*b%M;
}
static int nCr(int n, int r)
{
return fact(n) / (fact(r) *
fact(n - r));
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Returns factorial of n
static int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// to generate the lps array
// lps means longest preffix that is also a suffix
static void generateLPS(int lps[] , String p){
int l = 0;
int r = 1;
while(l < p.length() && l < r && r < p.length()){
if(p.charAt(l) == p.charAt(r)){
lps[r] = l + 1;
l++;
r++;
}
else{
if(l > 0)
l = lps[l - 1];
else
r++;
}
}
}
// returns the index of the element which is just smaller than or
// equal to the tar in the given arraylist
static int lowBound(ArrayList<Integer> ll,long tar ,int l,int r){
if(l>r) return l;
int mid=l+(r-l)/2;
if(ll.get(mid)>=tar){
return lowBound(ll,tar,l,mid-1);
}
return lowBound(ll,tar,mid+1,r);
}
// returns the index of the element which is just greater than or
// equal to the tar in the given arraylist
static int upBound(ArrayList<Integer> ll,long tar,int l ,int r){
if(l>r) return l;
int mid=l+(r-l)/2;
if(ll.get(mid)<=tar){
return upBound(ll,tar,mid+1 ,r);
}
return upBound(ll,tar,l,mid-1);
}
static void swap(int i , int j , int a[]){
int x = a[i];
int y = a[j];
a[j] = x;
a[i] = y;
}
// a -> z == 97 -> 122
// String.format("%.9f", ans) ,--> to get upto 9 decimal places , (ans is double)
// write
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 8acd824f749a3b25eeb38ca9c0a194d2 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.sql.SQLSyntaxErrorException;
import java.util.*;
import java.io.*;
import java.util.stream.StreamSupport;
public class Solution {
static long mod = (long)Math.pow(10,9)+7l;
public static void main(String str[]) throws IOException {
Reader sc = new Reader();
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
// Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
long arr[] = new long[n];
long ans = 0;
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
if(n>1) {
long pre = 0;
long last = arr[1];
int ind = 1;
for (int j = 2; j < n; j++, ind++) {
ans -= ((arr[j] * ind) - pre);
pre += last;
last = arr[j];
}
}
output.write(ans+"\n");
}
output.flush();
}
static int fun(ArrayList<Integer> al, int n){
if(n==0 || n==1) return 0;
int LIS[] = new int[n];
int len = 0;
// Mark all elements of LIS as 1
for (int i = 0; i < n; i++)
LIS[i] = 1;
// Find LIS of array
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (al.get(i) > al.get(j) && (i-j)<=(al.get(i)-al.get(j)))
LIS[i] = Math.max(LIS[i],
LIS[j] + 1);
}
len = Math.max(len, LIS[i]);
}
// Return min changes for array
// to strictly increasing
return n - len;
}
static long treeTraversal(Tree arr[], int parent, int x){
long tot = 0;
for(int i: arr[x].al){
if(i!=parent){
tot+=treeTraversal(arr, x, i);
}
}
arr[x].child = tot;
if(arr[x].child==0) arr[x].child = 1;
return tot+1;
}
public static int primeFactors(int n, int k)
{
int ans = 0;
while (n%2==0)
{
ans++;
if(ans>=k) return k;
n /= 2;
}
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
while (n%i == 0)
{
ans++;
n /= i;
if(ans>=k) return k;
}
}
if (n > 2) ans++;
return ans;
}
static int binaryLow(ArrayList<Integer> arr, int x, int s, int e){
if(s>=e){
if(arr.get(s)>=x) return s;
else return s+1;
}
int m = (s+e)/2;
if(arr.get(m)==x) return m;
if(arr.get(m)>x) return binaryLow(arr,x,s,m);
if(arr.get(m)<x) return binaryLow(arr,x,m+1,e);
return 0;
}
static int binaryLow(int[] arr, int x, int s, int e){
if(s>=e){
if(arr[s]>=x) return s;
else return s+1;
}
int m = (s+e)/2;
if(arr[m]==x) return m;
if(arr[m]>x) return binaryLow(arr,x,s,m);
if(arr[m]<x) return binaryLow(arr,x,m+1,e);
return 0;
}
static int binaryHigh(int[] arr, int x, int s, int e){
if(s>=e){
if(arr[s]<=x) return s;
else return s-1;
}
int m = (s+e)/2;
if(arr[m]==x) return m;
if(arr[m]>x) return binaryHigh(arr,x,s,m-1);
if(arr[m]<x) return binaryHigh(arr,x,m+1,e);
return 0;
}
static void arri(int arr[], int n, Reader sc) throws IOException{
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
}
static void arrl(long arr[], int n, Reader sc) throws IOException{
for(int i=0;i<n;i++){
arr[i] = sc.nextLong();
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long power(long x, long y, long p)
{
long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0)
return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// static boolean dfs(Tree node, boolean[] visited, int parent, ArrayList<Tree> tt){
// visited[node.a] = true;
// boolean b = false;
// for(int i: node.al){
// if(tt.get(i).a!=parent){
// if(visited[tt.get(i).a]) return true;
// b|= dfs(tt.get(i), visited, node.a, tt);
// }
// }
// return b;
// }
static class Pair{
// int ind;
long a;
long b;
// ArrayList<Integer> ii = new ArrayList<>();
Pair( long a, long b){
// this.ind = i;
this.a = a;
this.b = b;
}
}
static class SortbyI implements Comparator<Pair> {
// Used for sorting in ascending order of
// roll number
public int compare(Pair a, Pair b)
{
if(a.b>=b.b) return 1;
else return -1;
}
}
// static class SortbyD implements Comparator<Pair> {
// // Used for sorting in ascending order of
// // roll number
// public int compare(Pair a, Pair b)
// {
// return b.a - a.a;
// }
// }
static int binarySearch(ArrayList<Pair> a, int x, int s, int e){
if(s>=e){
if(x<=a.get(s).b) return s;
else return s+1;
}
int mid = (e+s)/2;
if(a.get(mid).b<x){
return binarySearch(a, x, mid+1, e);
}
else return binarySearch(a,x,s, mid);
}
// static class Edge{
// int a;
// int b;
// int c;
// int sec;
// Edge(int a, int b, int c, int sec){
// this.a = a;
// this.b = b;
// this.c = c;
// this.sec = sec;
// }
//
// }
static class Tree{
int a;
ArrayList<Integer> al = new ArrayList<>();
long curr = 0;
long freeSpace = 0;
long child = 0;
long max =0;
Tree(int a){
this.a = a;
}
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
static boolean isPrime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 ||
n % 3 == 0)
return false;
for (int i = 5;
i * i <= n; i = i + 6)
if (n % i == 0 ||
n % (i + 2) == 0)
return false;
return true;
}
static ArrayList<Integer> sieveOfEratosthenes(int n)
{
ArrayList<Integer> al = new ArrayList<>();
// Create a boolean array
// "prime[0..n]" and
// initialize all entries
// it as true. A value in
// prime[i] will finally be
// false if i is Not a
// prime, else true.
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
al.add(i);
}
return al;
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | ec17c004bed1c7ec6bfe43097a476edc | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
public class C {
// *** ++
// +=-==+ +++=-
// +-:---==+ *+=----=
// +-:------==+ ++=------==
// =-----------=++=========================
// +--:::::---:-----============-=======+++====
// +---:..:----::-===============-======+++++++++
// =---:...---:-===================---===++++++++++
// +----:...:-=======================--==+++++++++++
// +-:------====================++===---==++++===+++++
// +=-----======================+++++==---==+==-::=++**+
// +=-----================---=======++=========::.:-+*****
// +==::-====================--: --:-====++=+===:..-=+*****
// +=---=====================-... :=..:-=+++++++++===++*****
// +=---=====+=++++++++++++++++=-:::::-====+++++++++++++*****+
// +=======++++++++++++=+++++++============++++++=======+******
// +=====+++++++++++++++++++++++++==++++==++++++=:... . .+****
// ++====++++++++++++++++++++++++++++++++++++++++-. ..-+****
// +======++++++++++++++++++++++++++++++++===+====:. ..:=++++
// +===--=====+++++++++++++++++++++++++++=========-::....::-=++*
// ====--==========+++++++==+++===++++===========--:::....:=++*
// ====---===++++=====++++++==+++=======-::--===-:. ....:-+++
// ==--=--====++++++++==+++++++++++======--::::...::::::-=+++
// ===----===++++++++++++++++++++============--=-==----==+++
// =--------====++++++++++++++++=====================+++++++
// =---------=======++++++++====+++=================++++++++
// -----------========+++++++++++++++=================+++++++
// =----------==========++++++++++=====================++++++++
// =====------==============+++++++===================+++==+++++
// =======------==========================================++++++
// created by : Nitesh Gupta
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
String[] scn = (br.readLine()).trim().split(" ");
int n = Integer.parseInt(scn[0]);
long sum = 0;
long[] arr = new long[n];
scn = (br.readLine()).trim().split(" ");
long cost = 0;
for (int i = 0; i < n; i++) {
arr[i] = Long.parseLong(scn[i]);
}
long tot = 0;
long ans = 0;
sort(arr);
boolean pos = true;
for (int i = 2; i < n; i++) {
sum += arr[i - 2];
tot = arr[i];
if (tot < 0) {
pos = false;
}
ans += arr[i];
cost -= (arr[i] * (long)(i - 1));
cost += sum;
ans -= tot;
}
if (!pos) {
cost -= ans;
}
sb.append(cost);
sb.append("\n");
}
System.out.println(sb);
return;
}
public static void sort(long[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = (int) Math.random() * n;
long temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr);
}
public static void sort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = (int) Math.random() * n;
int temp = arr[i];
arr[i] = arr[idx];
arr[idx] = temp;
}
Arrays.sort(arr);
}
public static void print(long[][] dp) {
for (long[] a : dp) {
for (long ele : a) {
System.out.print(ele + " ");
}
System.out.println();
}
}
public static void print(int[][] dp) {
for (int[] a : dp) {
for (int ele : a) {
System.out.print(ele + " ");
}
System.out.println();
}
}
public static void print(int[] dp) {
for (int ele : dp) {
System.out.print(ele + " ");
}
System.out.println();
}
public static void print(long[] dp) {
for (long ele : dp) {
System.out.print(ele + " ");
}
System.out.println();
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | e75c591483fd3d165a50902b6df07747 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
public class greatgraph {
private static class pair implements Comparable<pair> {
int low, high;
pair(int low, int high) {
this.low = low;
this.high = high;
}
@Override
public int hashCode()
{
// uses roll no to verify the uniqueness
// of the object of Student class
final int temp = 14;
int ans = 1;
ans = (temp * ans) + low +high;
return ans;
}
public int compareTo(pair o)
{
return this.low-o.low;
}
// Equal objects must produce the same
// hash code as long as they are equal
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (this.getClass() != o.getClass()) {
return false;
}
pair other = (pair)o;
if (this.low != other.low && this.high !=other.high) {
return false;
}
return true;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar, snumChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String[] args) {
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int t = in.nextInt();
while (t-- != 0) {
int n=in.nextInt();
long dis[]=new long[n];
for(int i=1;i<=n;i++)
{
dis[i-1]=in.nextLong();
}
Arrays.sort(dis);
long sum=dis[n-1];long neg=0;
for(int i=1;i<n;i++)
{
neg=(neg+(dis[i]-dis[i-1])*i);sum-=neg;
// out.println(neg);
}
// out.println(neg);
out.println(sum);
}out.close();}}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | e8b121b1063648bad26d3d1beeb9924f | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | //import java.io.IOException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class GreatGraphs {
static InputReader inputReader=new InputReader(System.in);
static void solve()
{
int n=inputReader.nextInt();
long arr[]=new long[n];
for (int i=0;i<n;i++)
{
arr[i]=inputReader.nextLong();
}
Arrays.sort(arr);
long sum=0;
long ans=arr[n-1];
for (int i=0;i<n;i++)
{
if(i==0)
{
sum+=arr[i];
continue;
}
ans+=sum-((long) (arr[i] *i));
sum+=arr[i];
}
out.println(ans);
}
static PrintWriter out=new PrintWriter((System.out));
public static void main(String args[])throws IOException
{
int t=inputReader.nextInt();
while(t-->0)
{
solve();
}
out.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 302a2c6e84a9037120b473e522c700ae | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
int testCount = in.nextInt();
for (int t = 1; t <= testCount; t++){
//take from std input
int n = in.nextInt();
if(n < 3){
for(int i=0;i<n;i++)
in.nextInt();
out.println(0);
continue;
}
long[] arr = new long[n-1];
in.nextInt();
long ans = 0;
for(int i=0;i<n-1;i++){
arr[i] = in.nextLong();
ans += arr[i];
}
Arrays.sort(arr);
ans = ans - arr[n-2];
long ret = 0;
ret += (((n-2)*arr[n-2]) - ans);
for(int i=n-3;i>=0;i--){
ans -= arr[i];
ret += ((i+1)*arr[i] - ans);
//System.out.println(ret+"###"+ans);
}
out.println(-ret);
}
out.close();
}
static class Solver{
public void solve() {
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 96e8c8c75a40b8764346212e241b42f4 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
import java.io.*;
public class GreatGraphs {
PrintWriter out;
StringTokenizer st;
BufferedReader br;
final int imax = Integer.MAX_VALUE, imin = Integer.MIN_VALUE;
final int mod = 1000000007;
void solve() throws Exception {
int t = 1;
t = ni();
for (int ii = 0; ii < t; ii++) {
int n= ni();
List<Integer> list= new ArrayList<>();
for (int i = 0; i < n; i++) list.add(ni()); Collections.sort(list, Collections.reverseOrder());
long[] diff= new long[n];
for (int i = 0; i < n; i++) diff[i]= list.get(0)- list.get(i);
for (int i = n-2; i>= 0; i--) diff[i]+= diff[i+1];
long ans= list.get(0);
for (int i = 0; i < n; i++) ans-= (diff[i]- (list.get(0)- list.get(i)+0l)*(n- i));
out.println(ans);
}
}
public static void main(String[] args) throws Exception {
new GreatGraphs().run();
}
void run() throws Exception {
if (System.getProperty("ONLINE_JUDGE") == null) {
File file = new File("C:\\college\\CodeForces\\inputf.txt");
br = new BufferedReader(new FileReader(file));
out = new PrintWriter("C:\\college\\CodeForces\\outputf.txt");
} else {
out = new PrintWriter(System.out);
br = new BufferedReader(new InputStreamReader(System.in));
}
long ss = System.currentTimeMillis();
st = new StringTokenizer("");
while (true) {
solve();
String s = br.readLine();
if (s == null) break;
else st = new StringTokenizer(s);
}
//out.println(System.currentTimeMillis()-ss+"ms");
out.flush();
}
void read() throws Exception {
st = new StringTokenizer(br.readLine());
}
int ni() throws Exception {
if (!st.hasMoreTokens()) read();
return Integer.parseInt(st.nextToken());
}
char nc() throws Exception {
if (!st.hasMoreTokens()) read();
return st.nextToken().charAt(0);
}
String nw() throws Exception {
if (!st.hasMoreTokens()) read();
return st.nextToken();
}
long nl() throws Exception {
if (!st.hasMoreTokens()) read();
return Long.parseLong(st.nextToken());
}
int[] ni(int n) throws Exception {
int[] ret = new int[n];
for (int i = 0; i < n; i++) ret[i] = ni();
return ret;
}
long[] nl(int n) throws Exception {
long[] ret = new long[n];
for (int i = 0; i < n; i++) ret[i] = nl();
return ret;
}
double nd() throws Exception {
if (!st.hasMoreTokens()) read();
return Double.parseDouble(st.nextToken());
}
String ns() throws Exception {
String s = br.readLine();
return s.length() == 0 ? br.readLine() : s;
}
void print(int[] arr) {
for (int i : arr) out.print(i + " ");
out.println();
}
void print(long[] arr) {
for (long i : arr) out.print(i + " ");
out.println();
}
void print(int[][] arr) {
for (int[] i : arr) {
for (int j : i) out.print(j + " ");
out.println();
}
}
void print(long[][] arr) {
for (long[] i : arr) {
for (long j : i) out.print(j + " ");
out.println();
}
}
long add(long a, long b) {
if (a + b >= mod) return (a + b) - mod;
else return a + b >= 0 ? a + b : a + b + mod;
}
long mul(long a, long b) {
return (a * b) % mod;
}
void print(boolean b) {
if (b) out.println("YES");
else out.println("NO");
}
long binExp(long base, long power) {
long res = 1l;
while (power != 0) {
if ((power & 1) == 1) res = mul(res, base);
base = mul(base, base);
power >>= 1;
}
return res;
}
long gcd(long a, long b) {
if (b == 0) return a;
else return gcd(b, a % b);
}
// strictly smaller on left
void stack_l(int[] arr, int[] left) {
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < arr.length; i++) {
while (!stack.isEmpty() && arr[stack.peek()] >= arr[i]) stack.pop();
if (stack.isEmpty()) left[i] = -1;
else left[i] = stack.peek();
stack.push(i);
}
}
// strictly smaller on right
void stack_r(int[] arr, int[] right) {
Stack<Integer> stack = new Stack<>();
for (int i = arr.length - 1; i >= 0; i--) {
while (!stack.isEmpty() && arr[stack.peek()] >= arr[i]) stack.pop();
if (stack.isEmpty()) right[i] = arr.length;
else right[i] = stack.peek();
stack.push(i);
}
}
private void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i : arr) list.add(i);
Collections.sort(list);
for (int i = 0; i < arr.length; i++) arr[i] = list.get(i);
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | c821be86dd1fb2c9a904dcf841d6d024 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class C{
public static void main(String[] args){
FastReader sc = new FastReader();
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
long a[]=new long [n];
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
Arrays.sort(a);
long sum=a[n-1];
long neg[]=new long [n];
neg[0]=0;
for(int i=1;i<n;i++ ) {
neg[i]=neg[i-1]+((long)i*(long)(a[i]-a[i-1]));
sum-=neg[i];
}
System.out.println(sum);
}
}
static class ind {
int x;int y;
ind(int x,int y){
this.x=x;
this.y=y;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | cdd09de29fdc9ce42e25de7fe786827d | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
/*
*/
public class C {
static FastReader sc=null;
public static void main(String[] args) {
sc=new FastReader();
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int d[]=sc.readArray(n);
ruffleSort(d);
n--;
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=d[i+1]-d[i];
long ans=d[n];
for(int i=0;i<n;i++) {
long times=(long)(i+1)*(n-i);
ans-=times*a[i];
}
System.out.println(ans);
}
}
static int[] ruffleSort(int a[]) {
ArrayList<Integer> al=new ArrayList<>();
for(int i:a)al.add(i);
Collections.sort(al);
for(int i=0;i<a.length;i++)a[i]=al.get(i);
return a;
}
static void print(int a[]) {
for(int e:a) {
System.out.print(e+" ");
}
System.out.println();
}
static class FastReader{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while(!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
}
catch(IOException e){
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
return a;
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | e2487a9c09206b4d579f3421d505a4b9 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
/*
*/
public class C {
static FastReader sc=null;
public static void main(String[] args) {
sc=new FastReader();
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int d[]=sc.readArray(n);
ruffleSort(d);
if(n==1) {
System.out.println(0);
continue;
}
n--;
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=d[i+1]-d[i];
long ans=d[n];
for(int i=0;i<n;i++) {
long times=(long)(i+1)*(n-i);
ans-=times*a[i];
}
System.out.println(ans);
}
}
static int[] ruffleSort(int a[]) {
ArrayList<Integer> al=new ArrayList<>();
for(int i:a)al.add(i);
Collections.sort(al);
for(int i=0;i<a.length;i++)a[i]=al.get(i);
return a;
}
static void print(int a[]) {
for(int e:a) {
System.out.print(e+" ");
}
System.out.println();
}
static class FastReader{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while(!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
}
catch(IOException e){
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
return a;
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | d9deb1319468adb9b676bb65c30b94da | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.io.*;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class Main {
static InputReader sc = new InputReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
long[] d = new long[n];
for(int i = 0 ; i < n; i++){
d[i] = sc.nextLong();
}
shuffle(d);
Arrays.sort(d);
long ans = 0;
long tempSum = 0;
for(int i = 1 ; i < n; i++){
ans += d[i]*(1-i) - d[i-1] + tempSum;
tempSum +=d[i];
}
out.println(ans);
}
out.close();
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;}
String next()
{
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = reader.readLine();
}
catch (IOException e) {
throw new RuntimeException(e);
}
return str;
}
}
public static void shuffle(int[] a){
Random get = new Random();
for(int i = 0 ; i < a.length; i++){
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
public static void shuffle(long[] a){
Random get = new Random();
for(int i = 0 ; i < a.length; i++){
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 38a5f486e1f300d2dad5c89647ea5c29 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.io.*;
import java.util.*;
public class C
{
static ArrayList<Integer> g[];
public static void main(String[] args)throws IOException
{
FastReader f=new FastReader();
StringBuffer sb = new StringBuffer();
int test=f.nextInt();
while(test-->0)
{
int n=f.nextInt();
int a[]=new int[n];
long sum=0;
for(int i=0;i<n;i++)
{
a[i]=f.nextInt();
sum+=a[i];
}
long count=n-2;
Arrays.sort(a);
long ans=0;
for(int i=1;i<n-1;i++)
{
sum-=a[i];
ans+=sum-(count*(long)a[i-1]);
count--;
}
sb.append(-ans+"\n");
}
System.out.println(sb);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try{
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try{
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 50bccdb8f25243713a7f8e86abd9b495 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | //https://codeforces.com/contest/1541/problem/C
//C. Great Graphs
import java.util.*;
import java.io.*;
public class CF_1541_C{
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int t = Integer.parseInt(br.readLine());
while(t-->0){
int n = Integer.parseInt(br.readLine());
ArrayList<Integer> d = new ArrayList<Integer>(n);
st = new StringTokenizer(br.readLine());
for(int i=0;i<n;i++)
d.add(Integer.parseInt(st.nextToken()));
Collections.sort(d);
long cost = 0;
for(int i=1;i<n;i++){
long diff = d.get(i)-d.get(i-1);
cost += diff;
cost += 1L*(n-i)*i*(-diff);
}
sb.append(cost).append("\n");
}
pw.print(sb);
pw.flush();
pw.close();
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 42c9bf0b890bec7c44ac428882b44f36 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | // HOPE
///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
///
///
/// RRRRRRRRRRRRR YY YY AA NN NN
/// RR RR YY YY AA AA NN NN NN
/// RR RR YY YY AA AA NN NN NN
/// RRRRRRRRRR YY YY AAAAAAAAAAAA NN NN NN
/// RR RR YY AA AA NN NN NN
/// RR RR YY AA AA NN NNNN
/// RR RR YY AA AA NN NN
/// RR RR______________________________________________________________
////////
////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
public class Codechef {
static final int mod=1_000_000_007;
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader s = new FastReader();
StringBuilder sb = new StringBuilder();
int t = s.nextInt();
while(t-->0)
{
int n = s.nextInt();
long[] arr = new long[n];
for(int i=0;i<n;i++)
{
arr[i] = s.nextLong();
}
Arrays.sort(arr);
System.out.println(find(n, arr));
}
}
public static long find(int n, long[] arr)
{
long ans = arr[n-1];
long[] neg = new long[n];
for(int i=1;i<n;i++)
{
neg[i] = neg[i-1] + i*(arr[i]-arr[i-1]);
ans-=neg[i];
}
return ans;
}
/////////////////////////////////////////////////THE END///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public static int gcd(int x,int y)
{
return y==0?x:gcd(y,x%y);
}
public static long gcd(long x,long y)
{
return y==0L?x:gcd(y,x%y);
}
public static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static long pow(long a,long b)
{
if(b==0L)
return 1L;
long tmp=1;
while(b>1L)
{
if((b&1L)==1)
tmp*=a;
a*=a;
b>>=1;
}
return (tmp*a);
}
public static long modPow(long a,long b,long mod)
{
if(b==0L)
return 1L;
long tmp=1;
while(b>1L)
{
if((b&1L)==1L)
tmp*=a;
a*=a;
a%=mod;
tmp%=mod;
b>>=1;
}
return (tmp*a)%mod;
}
static long mul(long a, long b) {
return a*b%mod;
}
static long fact(int n) {
long ans=1;
for (int i=2; i<=n; i++) ans=mul(ans, i);
return ans;
}
static long fastPow(long base, long exp) {
if (exp==0) return 1;
long half=fastPow(base, exp/2);
if (exp%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
static long modInv(long x) {
return fastPow(x, mod-2);
}
static long nCk(int n, int k) {
return mul(fact(n), mul(modInv(fact(k)), modInv(fact(n-k))));
}
static boolean check(int n,int d)//check for digit 7 in 504732 number
{
String s = n+"";
return s.contains(d + "");
}
static void reverseArray(int[] a) {
int n = a.length;
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static void reverseArray(long[] a) {
int n = a.length;
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = a[n - i - 1];
for (int i = 0; i < n; i++)
a[i] = arr[i];
}
static class Pair{
int x, y;
Pair(int x, int y)
{
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "Pair{" +
"x=" + x +
", y=" + y +
'}';
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 966e17cd0ee093f585c9b5de2d672a72 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces {
public static void main(String[] args) throws IOException {
FastScanner fs = new FastScanner();
int t = fs.getInt();
while (t-- > 0) {
int n = fs.getInt();
long[] ar = fs.getLongArray(n);
Utility.sort(ar);
long sum = ar[n - 1];
long[] dp = new long[n];
dp[0] = 0;
for (int i = 1; i < n; i++) {
dp[i] = dp[i - 1] + i * (ar[i] - ar[i - 1]);
sum -= dp[i];
}
System.out.println(sum);
}
}
}
class ModularFunctions {
static long mod = 1000000007;
static long add(long x, long y) {
long result = x + y;
return result > mod ? result - mod : result;
}
static long sub(long x, long y) {
long result = x - y;
return result < 0 ? result + mod : result;
}
static long mul(long x, long y) {
long result = x * y;
return result >= mod ? result % mod : result;
}
static long pow(long x, long y) {
long result = 1;
x %= mod;
while (y > 0) {
if ((y & 1) == 1)
result = mul(result, x);
x = mul(x, x);
y = y >>> 1;
}
return result;
}
static long modInv(long x) {
return pow(x, mod - 2);
}
}
class Utility {
static int binarySearch(int[] ar, int x, int start, int end) {
int l = start;
int r = end;
while (l <= r) {
int mid = l + (r - l) / 2;
if (ar[mid] == x) {
return mid;
} else if (ar[mid] > x) {
r = mid - 1;
} else {
l = mid + 1;
}
}
return -1;
}
static int gcd(int a, int b) {
if (a > b) {
int t = a;
a = b;
b = t;
}
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lowerBound(int[] ar, int x) {
int l = 0;
int r = ar.length - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (ar[mid] >= x)
r = mid - 1;
else
l = mid + 1;
}
return r + 1;
}
static int upperBound(int[] ar, int x) {
int l = 0;
int r = ar.length - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (ar[mid] > x)
r = mid - 1;
else
l = mid + 1;
}
return l;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static boolean isPalindrome(String s) {
char[] ss = s.toCharArray();
int i = 0;
int j = s.length() - 1;
while (i < j) {
if (ss[i] != ss[j]) {
return false;
}
i += 1;
j -= 1;
}
return true;
}
static boolean isPrime(int n) {
if (n <= 1)
return false;
else if (n == 2)
return true;
else if (n % 2 == 0)
return false;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return false;
}
return true;
}
static List<Integer> sieveOfErathosthenes(int n) {
List<Integer> list = new ArrayList<>();
boolean[] prime = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p <= Math.sqrt(n); p++) {
if (prime[p]) {
for (int i = p * p; i <= n; i += p) {
prime[i] = false;
}
}
}
for (int i = 2; i <= n; i++)
if (prime[i])
list.add(i);
return list;
}
static void printArray(int[] ar) {
for (int i = 0; i < ar.length; i++) {
System.out.print(ar[i] + " ");
}
System.out.println();
}
static void printArray(long[] ar) {
for (int i = 0; i < ar.length; i++) {
System.out.print(ar[i] + " ");
}
System.out.println();
}
static void printList(List<Integer> list) {
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println();
}
static void printLongList(List<Long> list) {
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println();
}
static long choose(long n, long k) {
if (n < k)
return 0;
if (k == 0 || k == n)
return 1;
return choose(n - 1, k - 1) + choose(n - 1, k);
}
static List<Integer> divisors(int n) {
List<Integer> list = new ArrayList<>();
for (int i = 1; i * i < n; i++) {
if (n % i == 0)
list.add(i);
}
for (int i = (int) Math.sqrt(n); i >= 1; i--) {
if (n % i == 0)
list.add(n / i);
}
return list;
}
}
class Pair implements Comparable<Pair> {
Integer first;
Integer second;
Pair(Integer f, Integer s) {
this.first = f;
this.second = s;
}
public String toString() {
return "(" + this.first + ", " + this.second + ")";
}
@Override
public boolean equals(Object object) {
if (((Pair) object).first == this.first && ((Pair) object).second == this.second && object instanceof Pair) {
return true;
} else {
return false;
}
}
@Override
public int hashCode() {
return (String.valueOf(first) + ":" + String.valueOf(second)).hashCode();
}
@Override
public int compareTo(Pair p) {
int f = first.compareTo(p.first);
if (f != 0)
return f;
return Integer.compare(second, p.second);
}
}
class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int getInt() {
return Integer.parseInt(next());
}
long getLong() {
return Long.parseLong(next());
}
int[] getIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = getInt();
return a;
}
long[] getLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = getLong();
return a;
}
List<Integer> getIntList(int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(getInt());
return list;
}
List<Long> getLongList(int n) {
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(getLong());
return list;
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 247268097230ef1632aa2001ee10fbe7 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static int[] sort(int arr[]) {
List<Integer> list = new ArrayList<>();
for(int i:arr)
list.add(i);
Collections.sort(list);
for(int i = 0;i<list.size();i++) {
arr[i] = list.get(i);
}
return arr;
}
public static void main(String[] args) throws IOException{
FastScanner scan = new FastScanner();
int t = scan.nextInt();
for(int tt = 0;tt<t;tt++) {
int n = scan.nextInt();
int arr[] = scan.readArray(n);
sort(arr);
long nBefore = 0, sumBefore = 0, sum = 0;
for(int i = 1;i<n;i++) {
sum+=arr[i]-arr[i-1];
nBefore++;
sumBefore+=arr[i-1];
sum-= (arr[i]*nBefore - sumBefore);
}
System.out.println(sum);
}
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | f1d54b7a06fa6517b7c7757a5f8f68ea | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class GreatGraphs {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
Solver solver = new Solver();
while(t-- > 0)
{
int n = Integer.parseInt(br.readLine());
String s = br.readLine();
System.out.println(solver.solve(n, s));
}
}
}
class Solver{
public long solve(int n , String s)
{
String[] sa = s.split(" ");
long[] ia = new long[n];
int idx = 0;
for(String str : sa)
ia[idx++] = Long.parseLong(str);
Arrays.sort(ia); //o(NlogN)
long sum = ia[n-1]; //minimum positive path
long[] neg = new long[n];
neg[0] = 0;
for(int i = 1; i < n;i++)
{
neg[i] = neg[i-1] + i*(ia[i] - ia[i-1]);
sum -= neg[i];
}
return sum;
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | e96dcc43a214b1f3531990fa1a32e770 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | // package com.prituladima;
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.stream.IntStream;
public class C {
private void solve_() {
int n = nextInt();
long[] d = nextArrL(n);
if (n == 1 || n == 2) {
println(0);
} else {
long ans = 0L;
Arrays.sort(d);
for (int i = 1; i < n; i++) {
ans -= (d[i] - d[i - 1]) * (n - i) * i;
}
println(ans + d[n - 1]);
}
}
private void solve() {
int t = nextInt();
for (int i = 0; i < t; i++) {
solve_();
}
}
public static void main(String[] args) {
new C().run();
}
private BufferedReader reader;
private StringTokenizer tokenizer;
private PrintWriter writer;
private void run() {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out))) {
this.reader = reader;
this.writer = writer;
this.tokenizer = null;
solve();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
private int nextInt(int radix) {
return Integer.parseInt(nextToken(), radix);
}
private int nextInt() {
return Integer.parseInt(nextToken());
}
private long nextLong(int radix) {
return Long.parseLong(nextToken(), radix);
}
private long nextLong() {
return Long.parseLong(nextToken());
}
private double nextDouble() {
return Double.parseDouble(nextToken());
}
private int[] nextArr(int size) {
return Arrays.stream(new int[size]).map(c -> nextInt()).toArray();
}
private long[] nextArrL(int size) {
return Arrays.stream(new long[size]).map(c -> nextLong()).toArray();
}
private double[] nextArrD(int size) {
return Arrays.stream(new double[size]).map(c -> nextDouble()).toArray();
}
private char[][] nextCharMatrix(int n) {
return IntStream.range(0, n).mapToObj(i -> nextToken().toCharArray()).toArray(char[][]::new);
}
private int[][] nextIntMatrix(final int n, final int m) {
return IntStream.range(0, n).mapToObj(i -> nextArr(m)).toArray(int[][]::new);
}
private double[][] nextDoubleMatrix(final int n, final int m) {
return IntStream.range(0, n).mapToObj(i -> nextArrD(m)).toArray(double[][]::new);
}
private String nextToken() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
private void printf(String format, Object... args) {
writer.printf(format, args);
}
private void print(Object o) {
writer.print(o);
}
private void println() {
writer.println();
}
private void println(Object o) {
print(o);
println();
}
private void flush() {
writer.flush();
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 19111b8bc103624f7ed7111ce6a65b67 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
public class Main
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static long[] randomize(long arr[])
{
Random rand = new Random();
for (int i = 0; i < arr.length; ++i) {
int index = rand.nextInt(arr.length - i);
long tmp = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = arr[index];
arr[index] = tmp;
}
return arr;
}
static long mod = 1000000007;
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc = new FastReader();
PrintWriter out = new PrintWriter(System.out);
int test=sc.nextInt();
while(test-->0)
{
int n = sc.nextInt();
long a[] = new long[n];
for(int i=0;i<n;i++)
a[i] = sc.nextLong();
if(n<=2)
{
System.out.println(0);
}
else
{
a = randomize(a);
Arrays.sort(a);
long ps[] = new long[n];
ps[0] = 0;
for(int i=1;i<n;i++)
{
ps[i] = ps[i-1]+a[i];
}
long ans =0;
for(int i=2;i<n;i++)
{
long count = i-1;
ans = ans + (ps[i-2]-(count*a[i]));
}
System.out.println(ans);
}
}
out.flush();
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | fd85016e650b93dd536e463a348b51c8 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | /*
author: rohit_03012002
*/
import java.util.*;
import java.io.*;
import java.math.*;
import java.io.FileWriter;
import java.io.IOException;
public class cf{
static class Pair{
int u;
int v;
Pair(int a,int b){
u=a;
v=b;
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return ((u == other.u && v == other.v));
}
}
////////////////////////////////////**********Code-Start**********/////////////////////////////////////////////////
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
if (System.getProperty("ONLINE_JUDGE") == null) {
try {
System.setOut(new PrintStream(
new FileOutputStream("output.txt")));
sc = new InputReader(new FileInputStream("input.txt"));
}
catch (Exception e) {
}
int t=s(0);
while(t-->0)
solve();
}
else{
sc = new InputReader(inputStream);
pw = new PrintWriter(outputStream);
int t=s(0);
while(t-->0)
solve();
pw.close();
}
}
static ArrayList<ArrayList<Integer>>arr=new ArrayList<>();
// static PriorityQueue<Integer> pq=new PriorityQueue<>();
public static void solve() {
int n=s(0);
long a[]=new long[n];
feedArr(a);
if(n<=2){
System.out.println(0);
return;
}
long d=0,d1=0;
Arrays.sort(a);
for(int i=2;i<n;i++){
d+=a[i-2];
d1-=(long)(a[i]*(i-1));
d1+=d;
}
System.out.println(d1);
}
////////////////////////////////////**********Code-End**********//////////////////////////////////////////////////
// Arrays.sort(a,(c,b)->(c.x==b.x)?c.y-b.y:c.x-b.x); sort the array ont the basis of y if x1==x2
// set precision --> String.format("%.df",ans);
static int inf=Integer.MAX_VALUE;
static int minf=Integer.MIN_VALUE;
static InputReader sc;
static PrintWriter pw;
static ArrayList<ArrayList<Integer>> adj;
static int mod=1000000007;
static int mod1= 998244353;
static int max(int ...a){int m=a[0];for(int e:a)m=(m>=e)?m:e;return m;}
static int min(int ...a){int m=a[0];for(int e:a)m=(m<=e)?m:e;return m;}
static int abs(int a){return Math.abs(a);}
static long max(long ...a){long m=a[0];for(long e:a)m=(m>=e)?m:e;return m;}
static long min(long ...a){long m=a[0];for(long e:a)m=(m<=e)?m:e;return m;}
static long abs(long a){return Math.abs(a);}
static int ceil(double x){return (int)Math.ceil(x);}
static int floor(double x){return (int)Math.floor(x);}
static void subset(int a[],int n,ArrayList<Integer> arr){
if(n==a.length){
System.out.println(arr);
return;
}
ArrayList<Integer> b=new ArrayList<>();
ArrayList<Integer> c=new ArrayList<>();
b.addAll(arr);
c.addAll(arr);
b.add(a[n]);
subset(a,n+1,b);
subset(a,n+1,c);
}
static boolean prime[];
static int smallestFactor[];
static ArrayList<Integer> allPrimes;
static void sieveOfEratosthenes(int n)
{
prime = new boolean[n+1];
smallestFactor = new int[n+1];
allPrimes=new ArrayList<>();
smallestFactor[1] = 1;
for(int i=2;i<n;i++) {
prime[i] = true;
smallestFactor[i] = i;
}
for(int p = 2; p*p <=n; p++)
{
if(prime[p])
{
smallestFactor[p] = p;
for(int i = p*p; i <= n; i += p) {
prime[i] = false;
smallestFactor[i] = Math.min(p,smallestFactor[i]);
}
}
}
for(int i=0;i<prime.length;i++){
if(prime[i]){
allPrimes.add(i);
}
}
}
// static void initial(int n){
// for(int i=0;i<=n;i++){
// arr.add(new ArrayList<>());
// }
// }
static int numSum(int n){
int sum=0;
while(n!=0){
sum+=n%10;
n/=10;
}
return sum;
}
static void sort(ArrayList<Integer> brr){
Collections.sort(brr);
}
static void Lsort(ArrayList<Long> arr){
Collections.sort(arr);
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (Long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static boolean isperfect(double number)
{
double sqrt=Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static boolean isPrime(long n){
if(n<2){
return false;
}
for(int i = 2; i*i<=n; i++){
if(n%i==0){
return false;
}
}
return true;
}
static int lowerBound(int[] arr, int low, int high, long element) {
int middle;
while (low < high) {
middle = low + (high - low) / 2;
if (element > arr[middle])
low = middle + 1;
else
high = middle;
}
if (low < arr.length && arr[low] < element)
return -1;
return low;
}
static int upperBound(int[] arr, int low, int high, long element) {
int middle;
while (low < high) {
middle = low + (high - low) / 2;
if (arr[middle] > element)
high = middle;
else
low = middle + 1;
}
if (low<arr.length &&arr[low] <= element)
return -1;
return low;
}
public static String sortString(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
public static int ask(int l, int r){
System.out.println("? "+l+" "+r);
System.out.flush();
return sc.nextInt();
}
public static void swap(char []chrr, int i, int j){
char temp=chrr[i];
chrr[i]=chrr[j];
chrr[j]=temp;
}
public static void swap(int []chrr, int i, int j){
int temp=chrr[i];
chrr[i]=chrr[j];
chrr[j]=temp;
}
public static int countSetBits(int n){
int a=0;
while(n>0){
a+=(n&1);
n>>=1;
}
return a;
}
public static boolean isPowerOfTwo(long x){
return x!=0 && ((x&(x-1)) == 0);
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int lcm(int a , int b )
{
int gc = gcd(a,b);
return (a/gc)*b;
}
static long fast_pow(long base,long n,long M){
if(n==0)
return 1;
if(n==1)
return base;
long halfn=fast_pow(base,n/2,M);
if(n%2==0)
return ( halfn * halfn ) % M;
else
return ( ( ( halfn * halfn ) % M ) * base ) % M;
}
static long modInverse(long n,long M){
return fast_pow(n,M-2,M);
}
public static String reverse(String input)
{
StringBuilder str = new StringBuilder("") ;
for(int i =input.length()-1 ; i >= 0 ; i-- )
{
str.append(input.charAt(i));
}
return str.toString() ;
}
public static long nCr(int n,int k)
{
long ans=1L;
k=k>n-k?n-k:k;
int j=1;
for(;j<=k;j++,n--)
{
if(n%j==0)
{
ans*=n/j;
}else
if(ans%j==0)
{
ans=ans/j*n;
}else
{
ans=(ans*n)/j;
}
}
return ans;
}
public static long kadanesAlgorithm(long[] arr)
{
if(arr.length == 0)return 0 ;
long[] dp = new long[arr.length] ;
dp[0] = arr[0] ;
long max = dp[0] ;
for(int i = 1; i < arr.length ; i++)
{
if(dp[i-1] > 0)
{
dp[i] = dp[i-1] + arr[i] ;
}
else{
dp[i] = arr[i] ;
}
if(dp[i] > max)max = dp[i] ;
}
return max ;
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public static int s(int n){
return sc.nextInt();
}
public static long s(long n){
return sc.nextLong();
}
public static String s(String n){
return sc.next();
}
public static double s(double n){
return sc.nextDouble();
}
public static void p(int n){
pw.print(n);
}
public static void p(long n){
pw.print(n);
}
public static void p(String n){
pw.print(n);
}
public static void p(double n){
pw.print(n);
}
public static void pln(int n){
pw.println(n);
}
public static void pln(long n){
pw.println(n);
}
public static void pln(String n){
pw.println(n);
}
public static void pln(double n){
pw.println(n);
}
public static void feedArr(long []arr){
for(int i=0;i<arr.length;++i)
arr[i]=sc.nextLong();
}
public static void feedArr(double []arr){
for(int i=0;i<arr.length;++i)
arr[i]=sc.nextDouble();
}
public static void feedArr(int []arr){
for(int i=0;i<arr.length;++i)
arr[i]=sc.nextInt();
}
public static void feedArr(String []arr){
for(int i=0;i<arr.length;++i)
arr[i]=sc.next();
}
public static String printArr(int []arr){
StringBuilder sbr=new StringBuilder();
for(int i:arr)
sbr.append(i+" ");
return sbr.toString();
}
public static String printArr(long []arr){
StringBuilder sbr=new StringBuilder();
for(long i:arr)
sbr.append(i+" ");
return sbr.toString();
}
public static String printArr(String []arr){
StringBuilder sbr=new StringBuilder();
for(String i:arr)
sbr.append(i+" ");
return sbr.toString();
}
public static String printArr(double []arr){
StringBuilder sbr=new StringBuilder();
for(double i:arr)
sbr.append(i+" ");
return sbr.toString();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
// smallest lcm(a,b) => a+b=n will be n/smallPrimefactor ans n-(n/smallestprimefactor).
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 15aead9545f60843dfde7f7f74a90e57 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | //package round728.C;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
long sum1 = 0, sum2 = 0, sum3 = 0;
long[] arr = new long[n];
PriorityQueue<Long> pq = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
sum1 = Math.max(arr[i], sum1);
pq.add(arr[i]);
}
for (int i = 0; i < n; i++) {
long curr = pq.poll();
sum3 += Math.abs(curr);
sum2 += Math.abs(curr * (i + 1) - sum3);
}
System.out.println(sum1 - sum2);
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | cdbf65bebd1c627a00ba7d3d4dc4428b | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
import java.io.*;
public class C{
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main (String[] args) throws IOException {
int t = Integer.parseInt(br.readLine());
while(t-- > 0) {
int n = Integer.parseInt(br.readLine());
String ar[] = br.readLine().split(" ");
long arr[] = new long[n];
for(int i=0;i<n;i++) {
arr[i] = Long.parseLong(ar[i]);
}
Arrays.sort(arr);
long prefix[] = new long[n];
prefix[0] = arr[0];
for(int i=1;i<n;i++) {
prefix[i] = arr[i] + prefix[i-1];
}
long sum = 0;
for(int i=2;i<n;i++) {
sum += ((long)(i-1)*arr[i]) - prefix[i-2];
}
sum = 0 - sum;
bw.write(sum + "\n");
}
bw.flush();
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 69e779a0ac8203b4f01b3f74927de323 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
public class copy {
static long ans;
public static long[] inv(long[] arr,int l,int r)
{
if(l>r)
return null;
if(l==r)
{
long[] sum=new long[1];
sum[0]=arr[l];
return sum;
}
int mid=(l+r)/2;
long[] sum1=inv(arr,l,mid);
long[] sum2=inv(arr,mid+1,r);
long[] sum3=merge(arr,l,mid,r,sum1,sum2);
return sum3;
}
public static long[] merge(long[] arr,int l,int mid,int r,long[] sum1,long[] sum2)
{
//System.out.println(l+" "+mid+" "+r+" "+ans);
int N=mid-l+1;
int M=r-mid;
long[] arr1=new long[N];
long[] arr2=new long[N];
long[] mer=new long[N+M];
for(int i=0;i<N;i++)
arr1[i]=arr[l+i];
for(int i=0;i<M;i++)
arr2[i]=arr[mid+1+i];
int k=0;
int c=0,d=0;
while(c<N && d<M)
{
if(arr1[c]<=arr2[d])
{
mer[k++]=arr1[c++];
}
else
{
// System.out.println(arr2[d]+" hello "+arr1[c]);
// System.out.println(sum[mid]+" LAUDE "+sum[c]+" "+arr1[c]);
ans+=sum1[N-1]-sum1[c]+arr1[c];
mer[k++]=arr2[d++];
}
}
while(c<N)
mer[k++]=arr1[c++];
while(d<M)
mer[k++]=arr2[d++];
long s=0;
long[] sum=new long[N+M];
//System.out.println("SUM");
for(int i=0;i<N+M;i++)
{
s+=mer[i];
//System.out.print(s+" ");
sum[i]=s;
}
//System.out.println();
for(int i=0;i<mer.length;i++)
{
arr[l+i]=mer[i];
}
//System.out.println("ANSWER "+ans);
return sum;
}
public static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static void main(String[] args)throws IOException
{
Reader.init(System.in);
BufferedWriter output=new BufferedWriter(new OutputStreamWriter(System.out));
int T=Reader.nextInt();
for(int m=1;m<=T;m++) {
int N = Reader.nextInt();
ArrayList<Long> arr = new ArrayList<>();
for (int i = 1; i <= N; i++)
arr.add(Reader.nextLong());
Collections.sort(arr);
ArrayList<Long> sum = new ArrayList<>();
sum.add((long)0);
for (int i = 1; i < N; i++)
sum.add(arr.get(i) - arr.get(i - 1));
long s = 0;
long count;
for(int i=1;i<sum.size();i++)
{
count=(long)i*((long)N-(long)i);
s+=(count)*sum.get(i);
}
s-=arr.get(N-1);
//System.out.println(sum);
output.write((long)(-1)*s+"\n");
}
output.flush();
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
class cp
{
int a,b;
cp(int a,int b)
{
this.a=a;
this.b=b;
}
}
class sort_cp implements Comparator<cp>
{
public int compare(cp o1,cp o2)
{
return o1.a-o2.a;
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 9620f852e607a212b32f9ab3c4227976 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | /*
TO LEARN
1-segment trees
2-euler tour
3-fenwick tree and interval tree
*/
/*
TO SOLVE
uva 1103
*/
import java.util.*;
import java.math.*;
import java.io.*;
import java.util.stream.Collectors;
public class A{
static FastReader scan=new FastReader();
public static PrintWriter out = new PrintWriter (new BufferedOutputStream(System.out));
static int n;
static void brute()
{
int cnt[]=new int[100];
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
cnt[i+j]++;
}
for(int i=1;i<100;i++)
{
out.println(i+" "+cnt[i]);
}
}
public static void main(String[] args) throws Exception
{
// scan=new FastReader("cowcode.in");
//out = new PrintWriter("cowcode.out");
/*
currently doing
1-digit dp
2-ds like fenwick and interval tree and sparse table
*/
/*
READING
1-Everything About Dynamic Programming codeforces
2-DYNAMIC PROGRAMMING: FROM NOVICE TO ADVANCED topcoder
*/
int tt=1;
tt=scan.nextInt();
/*
n is odd
when you encounter n
n n+1 n+2 =the number that should e.x n=29 x=14
when n is even
n=x and not n+1 and n+2
*/
outer:while(tt-->0)
{
int n=scan.nextInt();
long arr[]=new long[n];
for(int i=0;i<n;i++)
arr[i]=scan.nextInt();
if(n<=2)
{
out.println(0);
continue outer;
}
sort(arr);
long sum = 0;
int idx=1;
for (int i = 1; i < n; i++)
{
sum =sum+(-arr[i]*idx);
sum=sum+(arr[i]*((n-1)-idx));
idx++;
}
for(int i=0;i<n-1;i++)
sum+=(arr[i+1]-arr[i]);
out.println(sum);
}
out.close();
}
static class special{
char x,y;
//int id;
special(char x,char y)
{
this.x=x;
this.y=y;
//this.id=id;
}
@Override
public int hashCode() {
return (int)(x + 31 * y);
}
@Override
public boolean equals(Object o){
if (o == this) return true;
if (o.getClass() != getClass()) return false;
special t = (special)o;
return t.x == x && t.y == y;
}
}
static long binexp(long a,long n)
{
if(n==0)
return 1;
long res=binexp(a,n/2);
if(n%2==1)
return res*res*a;
else
return res*res;
}
static long powMod(long base, long exp, long mod) {
if (base == 0 || base == 1) return base;
if (exp == 0) return 1;
if (exp == 1) return (base % mod+mod)%mod;
long R = (powMod(base, exp/2, mod) % mod+mod)%mod;
R *= R;
R %= mod;
if ((exp & 1) == 1) {
return (base * R % mod+mod)%mod;
}
else return (R %mod+mod)%mod;
}
static double dis(double x1,double y1,double x2,double y2)
{
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
static long mod(long x,long y)
{
if(x<0)
x=x+(-x/y+1)*y;
return x%y;
}
public static long pow(long b, long e) {
long r = 1;
while (e > 0) {
if (e % 2 == 1) r = r * b ;
b = b * b;
e >>= 1;
}
return r;
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long object : arr) list.add(object);
Collections.sort(list);
//Collections.reverse(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private static void sort2(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int object : arr) list.add(object);
Collections.sort(list);
Collections.reverse(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
public static class FastReader {
BufferedReader br;
StringTokenizer root;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
FastReader(String filename)throws Exception
{
br=new BufferedReader(new FileReader(filename));
}
boolean hasNext(){
String line;
while(root.hasMoreTokens())
return true;
return false;
}
String next() {
while (root == null || !root.hasMoreTokens()) {
try {
root = new StringTokenizer(br.readLine());
} catch (Exception addd) {
addd.printStackTrace();
}
}
return root.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception addd) {
addd.printStackTrace();
}
return str;
}
public int[] nextIntArray(int arraySize) {
int array[] = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = nextInt();
}
return array;
}
}
static class Pair implements Comparable<Pair>{
public long x, y,z;
public Pair(long x1, long y1,long z1) {
x=x1;
y=y1;
z=z1;
}
public Pair(long x1, long y1) {
x=x1;
y=y1;
}
@Override
public int hashCode() {
return (int)(x + 31 * y);
}
public String toString() {
return x + " " + y+" "+z;
}
@Override
public boolean equals(Object o){
if (o == this) return true;
if (o.getClass() != getClass()) return false;
Pair t = (Pair)o;
return t.x == x && t.y == y&&t.z==z;
}
public int compareTo(Pair o)
{
if(o.y>y)
return 1;
else if(o.y<y)
return -1;
else return 0;
}
}
static class tuple{
int x,y,z;
tuple(int a,int b,int c){
x=a;
y=b;
z=c;
}
}
static class Edge{
int d,w;
Edge(int d,int w)
{
this.d=d;
this.w=w;
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | c066e0d25d1b3417ce14f0862ade7ec6 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | // package com.company;
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main{
public static void main(String args[]){
InputReader in=new InputReader(System.in);
TASK solver = new TASK();
int t=1;
t = in.nextInt();
for(int i=1;i<=t;i++)
{
solver.solve(in,i);
}
}
static class TASK {
static int mod = 1000000007;
void solve(InputReader in, int testNumber) {
int n = in.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++)
{
a[i]=in.nextInt();
}
if(n==1)
{
System.out.println(0);
return;
}
Arrays.sort(a);
long ans=0;
long pre[] = new long[n];
pre[0]=a[0];
pre[1]=pre[0]+a[1];
for(int i=2;i<n;i++)
{
pre[i]=pre[i-1]+a[i];
ans-=((i-1)*1l*a[i]-pre[i-2]);
}
System.out.println(ans);
}
}
static class pair{
int x ;
int y;
pair(int x,int y)
{
this.x=x;
this.y=y;
}
}
static class Maths {
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static long factorial(int n) {
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c
== -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | ca28e3bfe28d5a6b2ba2096d5aae4e0e | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public final class C {
public static void main(String[] args) {
final FastScanner fs = new FastScanner();
final int t = fs.nextInt();
final StringBuilder sb = new StringBuilder();
for (int test = 0; test < t; test++) {
final int n = fs.nextInt();
final int[] arr = fs.nextIntArray(n);
Utils.shuffleSort(arr);
long res = 0;
long s = 0;
for (int i = 0; i < n - 2; i++) {
s += arr[i];
}
for (int i = 0; i < n - 2; i++) {
long c = (long) (n - 2 - i) * arr[n - 1 - i];
res += s - c;
s -= arr[n - 3 - i];
}
sb.append(res).append('\n');
}
System.out.println(sb);
}
static final class Utils {
private static class Shuffler {
private static void shuffle(int[] x) {
final Random r = new Random();
for (int i = 0; i <= x.length - 2; i++) {
final int j = i + r.nextInt(x.length - i);
swap(x, i, j);
}
}
private static void shuffle(long[] x) {
final Random r = new Random();
for (int i = 0; i <= x.length - 2; i++) {
final int j = i + r.nextInt(x.length - i);
swap(x, i, j);
}
}
private static void swap(int[] x, int i, int j) {
final int t = x[i];
x[i] = x[j];
x[j] = t;
}
private static void swap(long[] x, int i, int j) {
final long t = x[i];
x[i] = x[j];
x[j] = t;
}
}
public static void shuffleSort(int[] arr) {
Shuffler.shuffle(arr);
Arrays.sort(arr);
}
public static void shuffleSort(long[] arr) {
Shuffler.shuffle(arr);
Arrays.sort(arr);
}
private Utils() {}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
private String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] nextIntArray(int n) {
final int[] a = new int[n];
for (int i = 0; i < n; i++) { a[i] = nextInt(); }
return a;
}
long[] nextLongArray(int n) {
final long[] a = new long[n];
for (int i = 0; i < n; i++) { a[i] = nextLong(); }
return a;
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | e1a57fd16e9f3d9c46cb68ee4b78ffee | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
solve(sc, pw);
}
pw.close();
}
static void solve(Scanner in, PrintWriter out){
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
ruffleSort(arr);
long ans = 0;
long tot = 0;
for (int i = 1; i < n; i++) {
ans -= (i * 1l * arr[i] - tot - arr[i] + arr[i - 1]);
tot += arr[i];
}
out.println(ans);
}
// Use this instead of Arrays.sort() on an array of ints. Arrays.sort() is n^2
// worst case since it uses a version of quicksort. Although this would never
// actually show up in the real world, in codeforces, people can hack, so
// this is needed.
static void ruffleSort(int[] a) {
//ruffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n), temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 88eaf9881ca273dd952fadd049844646 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.*;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
int testNum = in.nextInt();
solver.solve(testNum, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
for (int z = 0; z < testNumber; z++) {
int n = in.nextInt();
ArrayList<Integer> d = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
d.add(in.nextInt());
}
Collections.sort(d, Collections.reverseOrder());
// out.println(d.toString());
long res = d.get(0);
for (int i = 0; i < n-1; i++) {
res -= ((long) (i+1)) * (n-1-i) * (d.get(i) - d.get(i+1));
}
out.println(res);
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | e50343274f4a01397db49ec2a3e5c67f | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
// static int mod = 998244353 ;
// static int N = 200005;
// static long factorial_num_inv[] = new long[N+1];
// static long natual_num_inv[] = new long[N+1];
// static long fact[] = new long[N+1];
// static void InverseofNumber()
//{
// natual_num_inv[0] = 1;
// natual_num_inv[1] = 1;
// for (int i = 2; i <= N; i++)
// natual_num_inv[i] = natual_num_inv[mod % i] * (mod - mod / i) % mod;
//}
//static void InverseofFactorial()
//{
// factorial_num_inv[0] = factorial_num_inv[1] = 1;
// for (int i = 2; i <= N; i++)
// factorial_num_inv[i] = (natual_num_inv[i] * factorial_num_inv[i - 1]) % mod;
//}
//static long nCrModP(long N, long R)
//{
// long ans = ((fact[(int)N] * factorial_num_inv[(int)R]) % mod * factorial_num_inv[(int)(N - R)]) % mod;
// return ans%mod;
//}
//static boolean prime[];
//static void sieveOfEratosthenes(int n)
//{
// prime = new boolean[n+1];
// for (int i = 0; i <= n; i++)
// prime[i] = true;
// for (int p = 2; p * p <= n; p++)
// {
// // If prime[p] is not changed, then it is a
// // prime
// if (prime[p] == true)
// {
// // Update all multiples of p
// for (int i = p * p; i <= n; i += p)
// prime[i] = false;
// }
// }
//}
static long dp[][];
static int visited[];
public static void main (String[] args) throws java.lang.Exception
{
// InverseofNumber();
// InverseofFactorial();
// fact[0] = 1;
// for (long i = 1; i <= 2*100000; i++)
// {
// fact[(int)i] = (fact[(int)i - 1] * i) % mod;
// }
FastReader scan = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
int t = scan.nextInt();
while(t-->0){
int n = scan.nextInt();
ArrayList<Long> al = new ArrayList<>();
long sum = 0;
long max = 0;
for(int i=1;i<=n;i++){
long x = scan.nextLong();
al.add(x);
sum = sum-x;
if(x>max)
max = x;
}
Collections.sort(al);
long temp = sum;
//pw.println(sum);
for(int i=1;i<n;i++){
temp = temp+(n-i)*(al.get(i)-al.get(i-1));
sum = sum+temp;
//pw.println(sum);
}
sum = sum+max;
pw.println(sum);
pw.flush();
}
}
//static long bin_exp_mod(long a,long n){
// long res = 1;
// if(a==0)
// return 0;
// while(n!=0){
// if(n%2==1){
// res = ((res)*(a));
// }
// n = n/2;
// a = ((a)*(a));
// }
// return res;
//}
//static long bin_exp_mod(long a,long n){
// long mod = 998244353;
// long res = 1;
// a = a%mod;
// if(a==0)
// return 0;
// while(n!=0){
// if(n%2==1){
// res = ((res%mod)*(a%mod))%mod;
// }
// n = n/2;
// a = ((a%mod)*(a%mod))%mod;
// }
// res = res%mod;
// return res;
//}
// static long gcd(long a,long b){
// if(a==0)
// return b;
// return gcd(b%a,a);
// }
// static long lcm(long a,long b){
// return (a/gcd(a,b))*b;
// }
}
//class Pair{
// long x,y;
// Pair(long x,long y){
// this.x = x;
// this.y = y;
// }
//}
// public boolean equals(Object obj) {
// // TODO Auto-generated method stub
// if(obj instanceof Pair)
// {
// Pair temp = (Pair) obj;
// if(this.x.equals(temp.x) && this.y.equals(temp.y))
// return true;
// }
// return false;
// }
// @Override
// public int hashCode() {
// // TODO Auto-generated method stub
// return (this.x.hashCode() + this.y.hashCode());
// }
//}
//class Compar implements Comparator<Pair>{
// public int compare(Pair p1,Pair p2){
// return p1.x-p2.x;
// }
//} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 2a73f77b74c95ca0b9db631dbd18038f | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.io.*;
import java.util.Arrays;
public class C {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
private static int nextInt() {
try {
in.nextToken();
} catch (IOException e) {
e.printStackTrace();
}
return (int) in.nval;
}
private static long nextLong() {
try {
in.nextToken();
} catch (IOException e) {
e.printStackTrace();
}
return (long) in.nval;
}
private static String next(){
try {
in.nextToken();
} catch (IOException e) {
e.printStackTrace();
}
return in.sval;
}
public static void main(String[] args) {
int t = nextInt();
for (int c = 0; c < t; c++) {
int n = nextInt();
int[] nums = new int[n+1];
for (int i = 1; i <= n; i++) {
nums[i] = nextInt();
}
Arrays.sort(nums);
long ans = 0;
for (int i = 3; i <= n; i++) {
ans += (long) nums[i] *(i-2);
}
for (int i = 1; i <= n-2; i++) {
ans -= (long) (n - i - 1) *nums[i];
}
System.out.println(-ans);
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | f04e55d657db131d73ca54b32565e4a5 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
import java.io.*;
import java.util.*;
public class GreatGraphs implements Runnable {
void solve() {
int t = ri();
while (t-- > 0) {
int n = ri();
long[] arr = lArr(n);
for (int i = 0; i < n; i++) {
arr[i] = rl();
}
shuffleSort(arr);
if (n <= 2) {
println(0);
} else {
long res = 0;
long sum = 0;
for (int i = n - 1; i > 0; i--) {
sum += arr[i] - arr[i - 1];
res += sum;
}
long lastDiff = res;
for (int i = n - 2; i >= 1; i--) {
long curr = lastDiff - (i + 1) * (arr[i + 1] - arr[i]);
res += curr;
lastDiff = curr;
}
res -= arr[n - 1];
println(-res);
}
}
}
static void shuffleSort(long[] aa) {
int n = aa.length;
Random rand = new Random();
for (int i = 1; i < n; i++) {
int j = rand.nextInt(i + 1);
long tmp = aa[i];
aa[i] = aa[j];
aa[j] = tmp;
}
Arrays.sort(aa);
}
/************************************************************************************************************************************************/
public static void main(String[] args) throws IOException {
new Thread(null, new GreatGraphs(), "1", 1 << 31).start();
}
static PrintWriter out = new PrintWriter(System.out);
static Reader read = new Reader();
static StringBuilder sbr = new StringBuilder();
static int mod = (int) 1e9 + 7;
static int dmax = Integer.MAX_VALUE;
static long lmax = Long.MAX_VALUE;
static int dmin = Integer.MIN_VALUE;
static long lmin = Long.MIN_VALUE;
static int[] dprimes = new int[] { 1, 11, 101, 1087, 99991, 100001, 1000003, 15485863, 999999937 };
@Override
public void run() {
solve();
out.close();
}
static class Reader {
private byte[] buf = new byte[1024];
private int index;
private InputStream in;
private int total;
public Reader() {
in = System.in;
}
public int scan() throws IOException {
if (total < 0)
throw new InputMismatchException();
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0)
return -1;
}
return buf[index++];
}
public int intNext() throws IOException {
int integer = 0;
int n = scan();
while (isWhiteSpace(n))
n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
} else
throw new InputMismatchException();
}
return neg * integer;
}
public double doubleNext() throws IOException {
double doub = 0;
int n = scan();
while (isWhiteSpace(n))
n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n) && n != '.') {
if (n >= '0' && n <= '9') {
doub *= 10;
doub += n - '0';
n = scan();
} else
throw new InputMismatchException();
}
if (n == '.') {
n = scan();
double temp = 1;
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
temp /= 10;
doub += (n - '0') * temp;
n = scan();
} else
throw new InputMismatchException();
}
}
return doub * neg;
}
public String read() throws IOException {
StringBuilder sb = new StringBuilder();
int n = scan();
while (isWhiteSpace(n))
n = scan();
while (!isWhiteSpace(n)) {
sb.append((char) n);
n = scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1)
return true;
return false;
}
}
static void print(Object object) {
out.print(object);
}
static void println(Object object) {
out.println(object);
}
static int[] iArr(int len) {
return new int[len];
}
static long[] lArr(int len) {
return new long[len];
}
static long min(long a, long b) {
return Math.min(a, b);
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long max(Long a, Long b) {
return Math.max(a, b);
}
static int max(int a, int b) {
return Math.max(a, b);
}
static int ri() {
try {
return read.intNext();
} catch (IOException e) {
e.printStackTrace();
}
System.exit(2);
return -1;
}
static long rl() {
try {
return Long.parseLong(read.read());
} catch (IOException e) {
e.printStackTrace();
}
System.exit(2);
return -1;
}
static String rs() {
try {
return read.read();
} catch (IOException e) {
e.printStackTrace();
}
System.exit(2);
return "";
}
static char rc() {
return rs().charAt(0);
}
static double rd() {
try {
return read.doubleNext();
} catch (IOException e) {
e.printStackTrace();
}
System.exit(2);
return -1;
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | d827830513145df569e5ea4e3ed42872 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[])throws Exception{
Input sc=new Input();
StringBuilder sb=new StringBuilder();
int t=sc.readInt();
for(int f=0;f<t;f++){
int n=sc.readInt();
int a[]=sc.readArray();
Arrays.sort(a);
long ans=0;
long dp[]=new long[n-1];
for(int i=0;i<n-1;i++){
if(i==0)
{
dp[i]=a[0];
}else
{
dp[i]=dp[i-1]+a[i+1];
}
//System.out.print(dp[i]+" ");
}
for(int i=0;i<n-1;i++){
ans+=(long)(dp[dp.length-1]-dp[i])-((long)a[i]*(long)(n-2-i));
//System.out.println(ans);
}
sb.append((-ans)+"\n");
}
System.out.print(sb);
}
}
class Input{
BufferedReader br;
StringTokenizer st;
Input(){
br=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
}
public int[] readArray() throws Exception{
st=new StringTokenizer(br.readLine());
int a[]=new int[st.countTokens()];
for(int i=0;i<a.length;i++){
a[i]=Integer.parseInt(st.nextToken());
}
return a;
}
public long[] readArrayLong() throws Exception{
st=new StringTokenizer(br.readLine());
long a[]=new long[st.countTokens()];
for(int i=0;i<a.length;i++){
a[i]=Long.parseLong(st.nextToken());
}
return a;
}
public int readInt() throws Exception{
st=new StringTokenizer(br.readLine());
return Integer.parseInt(st.nextToken());
}
public long readLong() throws Exception{
st=new StringTokenizer(br.readLine());
return Long.parseLong(st.nextToken());
}
public String readString() throws Exception{
return br.readLine();
}
public int[][] read2dArray(int n,int m)throws Exception{
int a[][]=new int[n][m];
for(int i=0;i<n;i++){
st=new StringTokenizer(br.readLine());
for(int j=0;j<m;j++){
a[i][j]=Integer.parseInt(st.nextToken());
}
}
return a;
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 109a52043bf989ca9cecca4d6006d365 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class GreatGraphs {
public static void main(String[] args) throws Exception {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int t = Integer.parseInt(buffer.readLine());
while (t-- > 0) {
int n = Integer.parseInt(buffer.readLine());
String [] inp = buffer.readLine().split(" ");
long [] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(inp[i]);
}
Arrays.sort(arr);
for (int i = 1; i < n; i++) {
arr[i] += arr[i-1];
}
long ans = 0;
for (int i = 2; i < n; i++) {
ans -= (i-1)*(arr[i]-arr[i-1])-arr[i-2];
}
sb.append(ans).append("\n");
}
System.out.println(sb);
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 0df6e1984a6af72087a9a7a7694e6a39 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
public class CF1541C {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while((t--) > 0)
{
int n = scanner.nextInt();
int[] d = new int[n+1];
long[] sum = new long[n+1];
for(int i=1;i<=n;i++)
d[i] = scanner.nextInt();
d[0] = 0;
Arrays.sort(d);
sum[0] = sum[1] = 0;
long ans = 0;
for(int i=2;i<=n;i++)
{
sum[i] = sum[i-1] + d[i];
ans += 1L * (i-2) * d[i] - sum[i-2];
}
System.out.println(-ans);
}
scanner.close();
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 7492714bef4573d4af1d537e1fbcb05c | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
public class codeforcesC2{
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while(st==null || !st.hasMoreElements())
{
try{
st=new StringTokenizer(br.readLine());
}
catch(IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
Double nextDouble(){
return Double.parseDouble(next());
}
float nextFloat(){
return Float.parseFloat(next());
}
}
public static void main(String args[]){
FastReader fs=new FastReader();
int t=fs.nextInt();
while(t-->0){
int n=fs.nextInt();
long a[]=new long[n];
for(int i=0;i<n;i++){
a[i]=fs.nextLong();
}
long pre[]=new long[n];
Arrays.sort(a);
pre[0]=0;
long ans=0;
for(int i=1;i<n;i++){
pre[i]=(long)pre[i-1]+(long)a[i];
ans+=(long)(pre[i-1]-(i*a[i]));
}
System.out.println(ans+a[n-1]);
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 2515bb1da3a349b549bd23f9aca47da6 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | // package Codeforces728;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args) {
FastScanner in = new FastScanner();
int T = in.nextInt();
for (int tt = 0; tt < T; tt++) {
int n = in.nextInt();
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextLong();
}
Arrays.sort(a);
long sum = 0;
for (int i = 1; i < n; i++) {
sum += a[i] - a[i - 1];
}
long pref[] = new long[n + 1];
for (int i = 1; i < n + 1; i++) {
pref[i] = pref[i - 1] + a[i - 1];
}
long ans = 0;
for (int i = 1; i < n + 1; i++) {
ans += (i * a[i - 1]) * 1l - pref[i] * 1l;
}
System.out.println(sum - ans);
}
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | f141610bb8cce8b49137f111371ec747 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
// import java.lang.*;
import java.io.*;
// THIS TEMPLATE MADE BY AKSH BANSAL.
public class Solution {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
private static boolean[] isPrime;
private static void primes(){
int num = (int)1e6; // PRIMES FROM 1 TO NUM
isPrime = new boolean[num];
for (int i = 2; i< isPrime.length; i++) {
isPrime[i] = true;
}
for (int i = 2; i< Math.sqrt(num); i++) {
if(isPrime[i] == true) {
for(int j = (i*i); j<num; j = j+i) {
isPrime[j] = false;
}
}
}
}
static void sort(int a[]){
ArrayList<Integer> arr=new ArrayList<>();
for(int i=0;i<a.length;i++)
arr.add(a[i]);
Collections.sort(arr);
for(int i=0;i<a.length;i++)
a[i]=arr.get(i);
}
static void sort(long a[]){
ArrayList<Long> arr=new ArrayList<>();
for(int i=0;i<a.length;i++)
arr.add(a[i]);
Collections.sort(arr);
for(int i=0;i<a.length;i++)
a[i]=arr.get(i);
}
private static long gcd(long a, long b){
if(b==0)return a;
return gcd(b,a%b);
}
private static long pow(long x,long y){
if(y==0)return 1;
long temp = pow(x, y/2);
if(y%2==1){
return x*temp*temp;
}
else{
return temp*temp;
}
}
// static ArrayList<Integer>[] adj;
// static void getAdj(int n,int q, FastReader sc){
// adj = new ArrayList[n+1];
// for(int i=1;i<=n;i++){
// adj[i] = new ArrayList<>();
// }
// for(int i=0;i<q;i++){
// int a = sc.nextInt();
// int b = sc.nextInt();
// adj[a].add(b);
// adj[b].add(a);
// }
// }
static PrintWriter out;
static FastReader sc ;
public static void main(String[] args) throws IOException {
sc = new FastReader();
out = new PrintWriter(System.out);
// primes();
// ________________________________
int t = sc.nextInt();
StringBuilder output = new StringBuilder();
while (t-- > 0) {
int n = sc.nextInt();
long[]arr = new long[n];
for(int i=0;i<n;i++){
arr[i] =sc.nextLong();
}
output.append(solver(n, arr)).append("\n");
}
out.println(output);
// _______________________________
// int n = sc.nextInt();
// out.println(solver());
// ________________________________
out.flush();
}
public static long solver(int n, long[] arr) {
long res = 0;
long cur = 0;
sort(arr);
for(int i=1;i<n;i++){
long temp= i*arr[i];
// System.out.println(cur+"__"+ temp);
res=res+cur-temp;
cur+=arr[i];
}
// long ans = 0;
// long tot = 0;
// for(int i = n - 1; i >= 0; i--) {
// ans += (n - i - 1) * arr[i] - tot;
// tot += arr[i];
// }
// ans += arr[n - 1];
// res = ans;
return res+arr[n-1];
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | bef41e4bd6ce9a6aff190dd392b1f25b | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-->0){
int n = s.nextInt();
long arr[] = new long[n];
for(int i=0;i<n;i++)arr[i] = s.nextLong();
Arrays.sort(arr);
long sm = arr[n-1];
long neg[] = new long[n];
neg[0] = 0l;
for(int i=1;i<n;i++){
neg[i] = neg[i-1]+ (arr[i]-arr[i-1])*i;
sm-=neg[i];
}
System.out.println(sm);
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 25dbe8210bd2ec3313342f85a2eb4031 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class CF1541C {
public static void main(String[] args) throws IOException {
int t = ri();
while (t --> 0) {
int n = ri(), d[] = ria(n);
rsort(d);
long ans = 0, pre = 0;
for (int i = 1; i < n; ++i) {
ans += d[i] - d[i - 1];
pre += d[i - 1];
ans -= (long) i * d[i] - pre;
}
prln(ans);
}
close();
}
static BufferedReader __i = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __o = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random __r = new Random();
// references
// IBIG = 1e9 + 7
// IMAX ~= 2e9
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final int IMIN = -2147483648;
static final long LMAX = 9223372036854775807L;
static final long LMIN = -9223372036854775808L;
// math util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int fli(double d) {return (int) d;}
static int cei(double d) {return (int) ceil(d);}
static long fll(double d) {return (long) d;}
static long cel(double d) {return (long) ceil(d);}
static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
static int[] exgcd(int a, int b) {if (b == 0) return new int[] {1, 0}; int[] y = exgcd(b, a % b); return new int[] {y[1], y[0] - y[1] * (a / b)};}
static long[] exgcd(long a, long b) {if (b == 0) return new long[] {1, 0}; long[] y = exgcd(b, a % b); return new long[] {y[1], y[0] - y[1] * (a / b)};}
static int randInt(int min, int max) {return __r.nextInt(max - min + 1) + min;}
static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
// array util
static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void rsort(int[] a) {shuffle(a); sort(a);}
static void rsort(long[] a) {shuffle(a); sort(a);}
static void rsort(double[] a) {shuffle(a); sort(a);}
static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
// input
static void r() throws IOException {input = new StringTokenizer(rline());}
static int ri() throws IOException {return Integer.parseInt(rline());}
static long rl() throws IOException {return Long.parseLong(rline());}
static double rd() throws IOException {return Double.parseDouble(rline());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;}
static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;}
static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;}
static char[] rcha() throws IOException {return rline().toCharArray();}
static String rline() throws IOException {return __i.readLine();}
static String n() {return input.nextToken();}
static int rni() throws IOException {r(); return ni();}
static int ni() {return Integer.parseInt(n());}
static long rnl() throws IOException {r(); return nl();}
static long nl() {return Long.parseLong(n());}
static double rnd() throws IOException {r(); return nd();}
static double nd() {return Double.parseDouble(n());}
// output
static void pr(int i) {__o.print(i);}
static void prln(int i) {__o.println(i);}
static void pr(long l) {__o.print(l);}
static void prln(long l) {__o.println(l);}
static void pr(double d) {__o.print(d);}
static void prln(double d) {__o.println(d);}
static void pr(char c) {__o.print(c);}
static void prln(char c) {__o.println(c);}
static void pr(char[] s) {__o.print(new String(s));}
static void prln(char[] s) {__o.println(new String(s));}
static void pr(String s) {__o.print(s);}
static void prln(String s) {__o.println(s);}
static void pr(Object o) {__o.print(o);}
static void prln(Object o) {__o.println(o);}
static void prln() {__o.println();}
static void pryes() {prln("yes");}
static void pry() {prln("Yes");}
static void prY() {prln("YES");}
static void prno() {prln("no");}
static void prn() {prln("No");}
static void prN() {prln("NO");}
static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;};
static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;}
static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;}
static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();}
static void h() {prln("hlfd"); flush();}
static void flush() {__o.flush();}
static void close() {__o.close();}} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 97e187c7f4ef2d6a9f37526fac70b251 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.SplittableRandom;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in Actual solution is at the top
*
* @author Pradyumn Agrawal (coderbond007)
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CGreatGraphs solver = new CGreatGraphs();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++) solver.solve(i, in, out);
out.close();
}
static class CGreatGraphs {
public void solve(int testNumber, FastScanner in, PrintWriter out) {
int n = in.nextInt();
long[] a = ArrayUtils.sort(in.nextLongArray(n));
long sum = 0;
long ans = 0;
for (int i = 0; i < n; ++i) {
ans -= i * a[i] - sum;
sum += a[i];
}
ans += a[n - 1];
out.println(ans);
}
}
static class FastScanner {
BufferedReader reader;
StringTokenizer tokenizer;
public FastScanner(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public long nextLong() {
return Long.parseLong(next());
}
}
static class ArrayUtils {
public static long[] sort(long[] a) {
a = shuffle(a, new SplittableRandom());
Arrays.sort(a);
return a;
}
public static long[] shuffle(long[] a, SplittableRandom gen) {
for (int i = 0, n = a.length; i < n; i++) {
int ind = gen.nextInt(n - i) + i;
long d = a[i];
a[i] = a[ind];
a[ind] = d;
}
return a;
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 85653df53ed5f0f56ffcc8ccca7e07ee | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
public class C{
public static void main(String[] args){
FS sc = new FS();
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
long[] a = sc.nextLongArray(n);
Arrays.sort(a);
// we can reach the pasture of max time at the end
// then we can visit all the -ve weighted roads as it
// takes no time to visit them but our answer will be minimized
// let 'd' be 0 2 3
// we visit all pastures till 3rd one
// then we visit 3-2, 3-1, 2-1 to minimize the solution
long max = a[n-1];
long[] neg = new long[n];
neg[0] = 0L;
for(int i=1; i<n; i++){
neg[i] = neg[i-1] + (i)*(a[i]-a[i-1]);
max -= neg[i];
}
pw.println(max);
}
pw.flush();
pw.close();
}
static class FS{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next(){
while(!st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
} catch(Exception ignored){
}
}
return st.nextToken();
}
int[] nextArray(int n){
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i] = nextInt();
}
return a;
}
long[] nextLongArray(int n){
long[] a = new long[n];
for(int i = 0; i < n; i++){
a[i] = nextLong();
}
return a;
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | b4845a5e83077f0122dd8930edfe4161 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int tc = stdin.nextInt();
for(int t = 0; t < tc; t++) {
int n = stdin.nextInt();
long[] a = new long[n];
for(int i = 0; i < n; i++) {
a[i] = stdin.nextLong();
}
Arrays.sort(a);
long ans = 0;
long tot = 0;
for(int i = n - 1; i >= 0; i--) {
ans += (n - i - 1) * a[i] - tot;
tot += a[i];
}
ans += a[n - 1];
System.out.println(ans);
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | f16a4f8de6939bc8a98e978e0d219af4 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int tc = stdin.nextInt();
for(int t = 0; t < tc; t++) {
int n = stdin.nextInt();
long[] a = new long[n];
for(int i = 0; i < n; i++) {
a[i] = stdin.nextLong();
}
Arrays.sort(a);
long ans = 0;
long tot = 0;
for(int i = n - 1; i >= 0; i--) {
ans += (n - i - 1) * a[i] - tot;
tot += a[i];
}
ans += a[n - 1];
System.out.println(ans);
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 1cb70a7dad8f77cf14be330be8fbaab1 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Code {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0)
{
int n = sc.nextInt();
long[] arr = new long[n];
for(int i=0; i<n; i++)
{
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
long res = arr[n-1];
// System.out.println(res);
long[] neg = new long[n];
neg[0] = 0;
for(int i=1; i<n;i++) {
// System.out.println(res);
neg[i] = neg[i-1]+i*(arr[i]-arr[i-1]);
res = res - neg[i];
//System.out.println(neg[i-1]+" "+neg[i]);
}
System.out.println(res);
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 48225118792dfebc163fa5b427c96b68 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Iterator;
public class Main {
private static final String NO = "No";
private static final String YES = "Yes";
InputStream is;
PrintWriter out;
String INPUT = "";
private static final long MOD = 1000000007;
static int MAXN = 10;
void solve() {
int T = ni();
for (int i = 0; i < T; i++)
solve(i);
}
void solve(int nth) {
int n = ni();
Integer[] a = na2(n);
long s = 0, ans = 0;
Arrays.sort(a);
for (int i = 0; i < n; ++i) {
if (i > 0)
ans += (long) a[i] * i - s;
s += a[i];
// tr(i, ans);
}
out.println(a[n - 1] - ans);
}
// a^b
long power(long a, long b) {
long x = 1, y = a;
while (b > 0) {
if (b % 2 != 0) {
x = (x * y) % MOD;
}
y = (y * y) % MOD;
b /= 2;
}
return x % MOD;
}
private long gcd(long a, long b) {
while (a != 0) {
long tmp = b % a;
b = a;
a = tmp;
}
return b;
}
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if (!INPUT.isEmpty())
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Main().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private char[] nc(int n) {
char[] ret = new char[n];
for (int i = 0; i < n; i++)
ret[i] = nc();
return ret;
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
// ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n) {
if (!(isSpaceChar(b)))
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private Integer[] na2(int n) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private int[][] na(int n, int m) {
int[][] a = new int[n][];
for (int i = 0; i < n; i++)
a[i] = na(m);
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private Long[] nl2(int n) {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private long[][] nl(int n, int m) {
long[][] a = new long[n][];
for (int i = 0; i < n; i++)
a[i] = nl(m);
return a;
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
public static class IterablePermutation implements Iterable<int[]>, Iterator<int[]> {
int[] a;
boolean first = true;
public IterablePermutation(int n) {
assert n >= 1;
a = new int[n];
for (int i = 0; i < n; i++)
a[i] = i;
}
public IterablePermutation(int... a) {
this.a = Arrays.copyOf(a, a.length);
}
@Override
public boolean hasNext() {
if (first)
return true;
int n = a.length;
int i;
for (i = n - 2; i >= 0 && a[i] >= a[i + 1]; i--)
;
return i != -1;
}
@Override
public int[] next() {
if (first) {
first = false;
return a;
}
int n = a.length;
int i;
for (i = n - 2; i >= 0 && a[i] >= a[i + 1]; i--)
;
assert i != -1;
int j;
for (j = i + 1; j < n && a[i] < a[j]; j++)
;
int d = a[i];
a[i] = a[j - 1];
a[j - 1] = d;
for (int p = i + 1, q = n - 1; p < q; p++, q--) {
d = a[p];
a[p] = a[q];
a[q] = d;
}
return a;
}
@Override
public Iterator<int[]> iterator() {
return this;
}
}
private static void tr(Object... o) {
System.out.println(Arrays.deepToString(o));
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | b2f107f94129c2c2649533fb0430bc22 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
public class C_Great_Graphs implements Runnable {
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new C_Great_Graphs(), "Main", 1 << 27).start();
}
static class Pair {
int f;
int s;
int p;
PrintWriter w;
// int t;
Pair(int f, int s) {
// Pair(int f,int s, PrintWriter w){
this.f = f;
this.s = s;
// this.p = p;
// this.w = w;
// this.t = t;
}
public static Comparator<Pair> wc = new Comparator<Pair>() {
public int compare(Pair e1,Pair e2){
// 1 for swap
if(Math.abs(e1.f)-Math.abs(e2.f)!=0){
// e1.w.println("**"+e1.f+" "+e2.f);
return (Math.abs(e1.f)-Math.abs(e2.f));
}
else{
// e1.w.println("##"+e1.f+" "+e2.f);
return (Math.abs(e1.s)-Math.abs(e2.s));
}
}
};
}
public Integer[] sort(Integer[] a) {
Arrays.sort(a);
return a;
}
public Long[] sort(Long[] a) {
Arrays.sort(a);
return a;
}
public static ArrayList<Integer> sieve(int N) {
int i, j, flag;
ArrayList<Integer> p = new ArrayList<Integer>();
for (i = 1; i < N; i++) {
if (i == 1 || i == 0)
continue;
flag = 1;
for (j = 2; j <= i / 2; ++j) {
if (i % j == 0) {
flag = 0;
break;
}
}
if (flag == 1) {
p.add(i);
}
}
return p;
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
public static int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
//// recursive dfs
public static int dfs(int s, ArrayList<Integer>[] g, long[] dist, boolean[] v, PrintWriter w, int p) {
v[s] = true;
int ans = 1;
// int n = dist.length - 1;
int t = g[s].size();
// int max = 1;
for (int i = 0; i < t; i++) {
int x = g[s].get(i);
if (!v[x]) {
// dist[x] = dist[s] + 1;
ans = Math.min(ans, dfs(x, g, dist, v, w, s));
} else if (x != p) {
// w.println("* " + s + " " + x + " " + p);
ans = 0;
}
}
// max = Math.max(max,(n-p));
return ans;
}
//// iterative BFS
public static int bfs(int s, ArrayList<Integer>[] g, long[] dist, boolean[] b, PrintWriter w, int p) {
b[s] = true;
int siz = 1;
// dist--;
Queue<Integer> q = new LinkedList<>();
q.add(s);
while (q.size() != 0) {
int i = q.poll();
Iterator<Integer> it = g[i].listIterator();
int z = 0;
while (it.hasNext()) {
z = it.next();
if (!b[z]) {
b[z] = true;
// dist--;
dist[z] = dist[i] + 1;
// siz++;
q.add(z);
} else if (z != p) {
siz = 0;
}
}
}
return siz;
}
public static int lower(int a[], int x) { // x is the target value or key
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] >= x)
r = m;
else
l = m;
}
return r;
}
public static int upper(int a[], int x) {// x is the key or target value
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] <= x)
l = m;
else
r = m;
}
return l + 1;
}
public static int lower(ArrayList<Integer> a, int x) { // x is the target value or key
int l = -1, r = a.size();
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a.get(m) >= x)
r = m;
else
l = m;
}
return r;
}
public static int upper(ArrayList<Integer> a, int x) {// x is the key or target value
int l = -1, r = a.size();
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a.get(m) <= x)
l = m;
else
r = m;
}
return l + 1;
}
public static long power(long x, long y, long m) {
if (y == 0)
return 1;
long p = power(x, y / 2, m) % m;
p = (p * p) % m;
if (y % 2 == 0)
return p;
else
return (x * p) % m;
}
public void yesOrNo(boolean f) {
if (f) {
w.println("YES");
} else {
w.println("NO");
}
}
// Arrays.sort(rooms, (room1, room2) -> room1[1] == room2[1] ? room1[0]-room2[0]
// : room2[1]-room1[1]);
// Arrays.sort(A, (a, b) -> Integer.compare(a[0] , b[0]));
//////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
// code here
int oo = (int) 1e9;
int[] parent;
int[] dist;
int[][] val;
boolean[][] vis;
ArrayList<Integer>[] g;
// int[] col;
// HashMap<Long, Boolean>[] dp;
//char[][] g;
// boolean[][] v;
Long[] a;
// ArrayList<Integer[]> a;
// int[][] ans;
long[][] dp;
long mod;
int n;
int m;
int k;
long[][] pre;
// StringBuilder[] a;
// StringBuilder[] b;
// StringBuilder ans;
int[][] col;
int[][] row;
PrintWriter w = new PrintWriter(System.out);
public void run() {
InputReader sc = new InputReader(System.in);
int defaultValue = 0;
mod = 1000000007;
int test = 1;
test = sc.nextInt();
while (test-- > 0) {
int n =sc.nextInt();
long[] d =new long[n];
long sum =0L;
for(int i=0;i<n;i++){
d[i] =sc.nextLong();
}
Arrays.sort(d);
if (n == 1 || n == 2) {
w.println(0);
}
else{
for(int i=1;i<n;i++){
sum -= (d[i] - d[i-1]) *(n-i)*i;
}
w.println(sum+d[n-1]);
}
}
w.flush();
w.close();
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | fb6aadb05f2c1b49c0efea73d8f8c034 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class GreatGraphOptimized {
//Fast Input
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//Printing an array
static void print(long[][] a, int r, int c) {
PrintWriter pw = new PrintWriter(System.out);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
pw.print(a[i][j] + " ");
}
pw.println();
}
pw.flush();
}
//Used to generate random test cases
//Must be changed according to input format
static void testCaseGenerator(int t, int lowN, int highN, long lowA, long highA) throws FileNotFoundException {
File input = new File("C:\\Users\\debar\\OneDrive\\Documents\\input.txt");
PrintWriter pw = new PrintWriter(input);
pw.println(t);
for (int i = 0; i < t; i++) {
int n = (int) (Math.random() * (highN - lowN)) + lowN;
pw.println(n);
for (int j = 0; j < n; j++) {
long a = (long) (Math.random() * (highA - lowA)) + lowA;
pw.print(a + " ");
}
pw.println();
}
pw.flush();
}
//Find factorial of numbers from 1 to n
//Check the size of factorial array
static long[] factorial(int n, long mod) {
long[] factorial = new long[n];
factorial[0] = 1;
for (int i = 1; i <= n; i++) {
factorial[i] = (factorial[i - 1] * i) % mod;
}
return factorial;
}
//Find factorial inverse from 1 to n
//Check the size of inverse_factorial array
static long[] factorialInverse(int n, long mod) {
long[] inverse_factorial = modularInverse(n, mod);
for (int i = 2; i <= n; i++) {
inverse_factorial[i] = (inverse_factorial[i] * inverse_factorial[i - 1]) % mod;
}
return inverse_factorial;
}
//Inverse exist when two number are relatively prime
//Based on Fermat little theorem (a^mod-1)%mod=1 when mod is prime
//a * a^(mod - 2) % mod = 1
//a^(mod - 2) % mod =a^-1
//When mod != prime use Extended euclidean theorem to calculate x and y for equation a*x + mod*y = gcd(a,b)=1
//(a*x + mod*y = gcd(a,b)=1) % mod = a*x %mod =1
//https://www.geeksforgeeks.org/find-initial-integral-solution-of-linear-diophantine-equation-if-finite-solution-exists/?ref=rp
//Used for only one number
static long modularInverse(long a, long mod) {
return binaryExponentiation(a, mod - 2, mod);
}
//Modular inverse for number from 1 to n - 1
//Time complexity O(n) for n numbers
//Proof https://cp-algorithms.com/algebra/module-inverse.html
static long[] modularInverse(int n, long mod) {
long[] inv = new long[n];
inv[1] = 1;
for (int i = 2; i < n; i++) {
inv[i] = mod - (mod / i) * inv[(int) (mod % i)] % mod;
}
return inv;
}
//GCD calculation of a and b with complexity O(log(Max(a,b))
static long gcd(long a, long b) {
long r;
while (b != 0) {
r = a % b;
a = b;
b = r;
}
return a;
}
//Find prime number upto n with complexity O(n*log(log(n)))
//Travel through the multiple of prime number and set it to false
static boolean[] sieveOfEratosthenes(int n) {
boolean[] prime = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (prime[p]) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
prime[0] = prime[1] = false;
return prime;
}
//Find Prefix sum in O(n)
static long[] PrefixSum(long[] arr, int n) {
long[] prefixSum = new long[n];
prefixSum[0] = arr[0];
for (int i = 1; i < n; ++i)
prefixSum[i] = prefixSum[i - 1] + arr[i];
return prefixSum;
}
//Find a^n in O(log n)
//If n=11 then n in binary is equal to 1011
//a^n=a^(8+2+1) that is multiply with result when bit is set.
//https://youtu.be/L-Wzglnm4dM
static long binaryExponentiation(long a, long n, long mod) {
long result = 1L;
n = n % (mod - 1);
//n = (mod - 1) * floor(n / (mod - 1)) + n % (mod - 1)
//a^((mod - 1) * floor(n / (mod - 1))) % mod = 1 so a^n % mod =a^(n % (mod - 1)) % mod when mod is prime
//Using fermat little theorem a^(p-1) % p = 1 when p is prime
while (n > 0) {
if ((n & 1) == 1) {
result = (result * a) % mod;
}
a = (a * a) % mod;
n /= 2;
}
return result;
}
//Matrix multiplication
static long[][] matrixMultiply(long[][] a, long[][] b, int row, int col, int mid, long mod) {
long[][] c = new long[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
c[i][j] = 0;
for (int k = 0; k < mid; k++) {
c[i][j] = (c[i][j] + (a[i][k] * b[k][j]) % mod) % mod;
}
}
}
return c;
}
//Matrix Exponentiation
static long[][] matrixExponentiation(long[][] a, long n, int size, long mod) {
if (n == 0) {
long[][] temp = new long[size][size];
for (int i = 0; i < size; i++) {
temp[i][i] = 1;
}
return temp;//Returns identity matrix
} else {
long[][] temp = matrixExponentiation(a, n / 2, size, mod);
if (n % 2 == 0) {
return matrixMultiply(temp, temp, size, size, size, mod);
} else {
return matrixMultiply(matrixMultiply(temp, temp, size, size, size, mod), a, size, size, size, mod);
}
}
}
//Solution starts here
public static void main(String[] args) {
//Input output class reference
FastReader sc = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
//Number of test case
int t = sc.nextInt();
//For each test case
for (int i = 0; i < t; i++) {
int n = sc.nextInt();
long a[]=new long[n];
for (int j = 0; j < n; j++)
{
a[j]= sc.nextLong();
}
//We can sort the distance from vertex 0 to other vertex and can assume that v0 + v1 = v2, v1 + v2 = v3 ...
Arrays.sort(a);
long count=0,sum=0;
for(int j=1;j<n;j++)
{
count+=a[j]-a[j-1];//This form a MST ( in this case a path)
}
//Now we need to add as many negative edges possible
for(int j=1;j<n;j++)
{
//Notice the symmetry of the adjacency matrix and the graph plotted
count-=(j*a[j]-sum);
sum+=a[j];
}
pw.println(count);
}
pw.flush();
}
//Solution ends here
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | c50459aafe34b45af250338dcd89de75 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Hello1 {
public static void main(String[] args) throws IOException {
FastReader s = new FastReader();
long t = s.nextInt();
for(int i = 0; i < t; i++) {
int n = s.nextInt();
long[] arr = new long[n];
for(int j = 0; j < n; j++) {
arr[j] = s.nextLong();
}
Arrays.sort(arr);
long[] ans = new long[n];
//System.out.println(Arrays.toString(arr));
//long[] ans = new long[n];
long an = 0;
if(arr.length >= 3) {
ans[2] = -1 * arr[2];
an = ans[2];
}
for(int j = 3; j < n; j++) {
ans[j] = -1 * (arr[j] - arr[j - 2]) + (arr[j] - arr[j - 1]) * (2 - j) + ans[j - 1];
an = ans[j] + an;
}
//System.out.println(Arrays.toString(ans));
System.out.println(an);
}
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 31b63536765aedd12e8d3a9ecafd3a35 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author wilso
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class codeforces {
static final long MOD2 = 998_244_353;
static final long X = 10000000000L;
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter pw = new PrintWriter(System.out);
// Scanner sc = new Scanner(System.in);
int tc = sc.ni();
// int tc = 1;
for (int rep = 0; rep < tc; rep++) {
int N = sc.ni();
int[] arr = sc.intArray(N);
sort(arr);
long ret = 0;
long rs = 0;
for (int i = 1; i < N; i++) {
rs += arr[i-1];
ret += arr[i] - arr[i-1];
ret -= arr[i] *(long)( i ) - rs;
}
pw.println(ret);
}
pw.close();
}
static int greaterThan(long[] arr, long target) {
int lo = 0, hi = arr.length- 1;
while(lo < hi) { // observe this time it is "<" operator, not "<="
int mid = lo + (hi-lo)/2;
if(arr[mid] <= target) lo = mid+1;
else hi = mid;
}
return arr[lo] > target ? lo : -1;
}
static int smallerThan(long[] arr, long target) {
int lo = 0, hi = arr.length- 1;
while(lo < hi) {
int mid = lo + (hi-lo+1)/2; // Note that I used ceil here otherwise it could go in infinite loop
if(arr[mid] < target) lo = mid;
else hi = mid-1;
}
return arr[lo] < target ? lo : -1;
}
static int[] reduce(int[] temp) {
int[] frac = new int[] {temp[0], temp[1]};
int curr = gcd(frac[0], frac[1]);
while (curr != 1) {
frac[0] /= curr;
frac[1] /= curr;
curr = gcd(frac[0], frac[1]);
}
return frac;
}
static int summation(int x) {
return x * (x+1) / 2;
}
static long summation(long x) {
return x*(x+1) / 2;
}
static long pow(long num, long exp, long mod){
long ans=1;
for(int i=1;i<=exp;i++){
ans=(ans*num)%mod;
}
return ans;
}
static boolean isPrime(int n)
{
if (n <= 1)
return false;
else if (n == 2)
return true;
else if (n % 2 == 0)
return false;
for (int i = 3; i <= Math.sqrt(n); i += 2) {
if (n % i == 0)
return false;
}
return true;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
public static void sort(int[] arr) {
Random rgen = new Random();
for (int i = 0; i < arr.length; i++) {
int r = rgen.nextInt(arr.length);
int temp = arr[i];
arr[i] = arr[r];
arr[r] = temp;
}
Arrays.sort(arr);
}
public static void sort(long[] arr) {
Random rgen = new Random();
for (int i = 0; i < arr.length; i++) {
int r = rgen.nextInt(arr.length);
long temp = arr[i];
arr[i] = arr[r];
arr[r] = temp;
}
Arrays.sort(arr);
}
static int charint(char c) {
return Integer.parseInt(String.valueOf(c));
}
/* */
//printing methods
/* */
//WOW!
/* */
public static void printArr(PrintWriter pw, int[] arr) {
StringBuilder sb = new StringBuilder();
for (int x : arr) {
sb.append(x + "");
}
sb.setLength(sb.length() - 1);
pw.println(sb.toString());
}
public static void printArr2d(PrintWriter pw, int[][] arr) {
StringBuilder sb = new StringBuilder();
for (int[] row : arr) {
for (int x : row) {
sb.append(x + " ");
}
sb.setLength(sb.length() - 1);
sb.append("\n");
}
pw.println(sb.toString());
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in), 32768);
st = null;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(next());
}
int[] intArray(int N) {
int[] ret = new int[N];
for (int i = 0; i < N; i++)
ret[i] = ni();
return ret;
}
long nl() {
return Long.parseLong(next());
}
long[] longArray(int N) {
long[] ret = new long[N];
for (int i = 0; i < N; i++)
ret[i] = nl();
return ret;
}
double nd() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
class MultiSet {
TreeMap<Long, Integer> map = new TreeMap<>();
private int size = 0;
public MultiSet() {
}
public void add(Long val) {
map.put(val, map.getOrDefault(val, 0) + 1);
size++;
}
public void remove(Long val) {
map.put(val, map.get(val) - 1);
if (map.get(val) == 0) {
map.remove(val);
}
size--;
}
public int size() {
return size;
}
public Long higher(Long val) {
return map.higherKey(val);
}
public Long lower(Long val) {
return map.lowerKey(val);
}
public Long ceiling(Long val) {
return map.ceilingKey(val);
}
public Long floor(Long val) {
return map.floorKey(val);
}
public Long first() {
return map.firstKey();
}
public Long last() {
return map.lastKey();
}
public boolean isEmpty() {
return map.isEmpty();
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | a040e1f04b83ff4893fb69d2547b30f7 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | /*input
3
3
0 2 3
2
0 1000000000
4
0 2 1 6
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static PrintWriter out;
static int MOD = 1000000007;
static FastReader scan;
/*-------- I/O usaing short named function ---------*/
public static String ns(){return scan.next();}
public static int ni(){return scan.nextInt();}
public static long nl(){return scan.nextLong();}
public static double nd(){return scan.nextDouble();}
public static String nln(){return scan.nextLine();}
public static void p(Object o){out.print(o);}
public static void ps(Object o){out.print(o + " ");}
public static void pn(Object o){out.println(o);}
/*-------- for output of an array ---------------------*/
static void iPA(int arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void lPA(long arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void sPA(String arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void dPA(double arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
/*-------------- for input in an array ---------------------*/
static void iIA(int arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ni();
}
static void lIA(long arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nl();
}
static void sIA(String arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ns();
}
static void dIA(double arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nd();
}
/*------------ for taking input faster ----------------*/
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
// Method to check if x is power of 2
static boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0);}
//Method to return gcd of two numbers
static int gcd(int a, int b){return a==0?b:gcd(b % a, a); }
//Method to count digit of a number
static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);}
//Method for sorting
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//Method for sorting
static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//Method foAr checking if a number is prime or not
static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
OutputStream outputStream =System.out;
out =new PrintWriter(outputStream);
scan =new FastReader();
//for fast output sometimes
StringBuilder sb = new StringBuilder();
int t = ni();
while(t-->0){
int n = ni();
long arr[] = new long[n];
lIA(arr);
sort(arr);
long psum[] = new long[n];
long sum = 0;
long diff[] = new long[n];
for(int i=1; i<n; i++){
diff[i] = arr[i] - arr[i-1];
}
//lPA(diff);
for(int i=0; i<n; i++){
sum += diff[i];
psum[i] = sum;
}
long ans = -sum;
//pn(ans);
//lPA(psum);
long tsum[] = new long[n];
/*sum = 0;
for(int i=0; i<n; i++){
sum += psum[i];
tsum[i] = sum;
}
lPA(tsum);
for(int i=0; i<n; i++){
if(tsum[n-1] - tsum[i] > 0){
ans += (tsum[n-1] - (n-i-1)*tsum[i]);
}
pn(ans);
}*/
//sum = 0;
long total = 0;
for(int i=n-1; i>=1; i--){
total += (n - i)*diff[i];
ans += total;
}
pn(-ans);
}
out.flush();
out.close();
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | ea83fc99e40873d6029d8fe91e665354 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class CodeForces {
static reader input = new reader();
static PrintWriter output = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
//BufferedReader bf = new BufferedReader(new FileReader("input.txt"));
//PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
//StringTokenizer stk = new StringTokenizer(bf.readLine());
//int n=Integer.parseInt(stk.nextToken());
C();
output.close();
}
public static void C(){
int t=input.nextInt();
for(int i=0;i<t;i++){
int n=input.nextInt();
long arr[]=input.nextLongArray(n);
if(n==1){
output.println(arr[0]);
}else{
Arrays.sort(arr);
long dp[]=new long[n];
long ans=arr[n-1];
for(int j=1;j<n;j++){
dp[j]=dp[j-1]+(j)*(arr[j]-arr[j-1]);
ans-=dp[j];
}
output.println(ans);
}
}
}
public static void D(){
}
public static void E(){
}
public static void F(){
}
public static long factorial(int a){
long ans=1;
for(int i=1;i<=a;i++){
ans*=i;
}
return ans;
}
public static long LCM(long a,long b){
return (a*b)/GCD(a,b);
}
public static long GCD(long a,long b){
if(a==0)
return b;
else if(b==0)
return a;
else
return GCD(b%a,a);
}
public static boolean isPrime(long num){
if(num==1)
return false;
else if(num==2||num==3)
return true;
else if(num%2==0||num%3==0)
return false;
else{
for(long i=5;i*i<=num;i+=6){
if(num%i==0||num%(i+2)==0)
return false;
}
}
return true;
}
public static ArrayList<Integer> SieveofEratothenis(){
ArrayList<Integer>ans=new ArrayList<>();
boolean visited[]=new boolean[1000000];
for(int i=2;i<1000000;i++){
if(!visited[i]){
ans.add(i);
for(long j=(long)i*i;j<1000000;j+=i){
visited[(int)j]=true;
}
}
}
return ans;
}
public static void mergesort(long arr[],int start,int end){//start and end must be indexes
if(start<end) {
int mid=(start+end)/2;
mergesort(arr,start,mid);
mergesort(arr, mid+1, end);
merge(arr, start,mid,end);
}
}
public static void merge(long arr[],int start,int mid,int end){
int lsize=mid-start+1,rsize=end-mid;
long l[]=new long[lsize],r[]=new long[rsize];
for(int i=start;i<=mid;i++){
l[i-start]=arr[i];
}
for(int i=mid+1;i<=end;i++){
r[i-mid-1]=arr[i];
}
int i=0,j=0,k=start;
while(i<lsize&&j<rsize){
if(l[i]<=r[j]){
arr[k++]=l[i++];
}else{
arr[k++]=r[j++];
}
}
while(i<lsize)
arr[k++]=l[i++];
while(j<rsize)
arr[k++]=r[j++];
}
}
class Value{
int ans;
}
class Pair{
int index,val;
Pair(int index,int val){
this.index=index;
this.val=val;
}
}
class reader {
BufferedReader br;
StringTokenizer st;
public reader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] nextIntArray(int arraySize) {
int array[] = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = nextInt();
}
return array;
}
public long[] nextLongArray(int arraySize) {
long array[] = new long[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = nextLong();
}
return array;
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 23f271ee69d6e1bde9f2993cf77c6194 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
public static void main (String[] args) throws IOException
{
OutputWriter log = new OutputWriter(System.out);
Reader sc=new Reader();
int t= sc.nextInt();
for(int time=0;time<t;time++)
{
int n= sc.nextInt();
Integer a[]=new Integer[n];
for(int i=0;i<n;i++)
{
a[i]= sc.nextInt();
}
Arrays.sort(a);
long ans=0;
long curr=0;
for(int i=0;i<n-2;i++)
{
curr=curr+(long)a[i];
ans= ans+curr;
}
for(int i=1;i<n-1;i++)
{
ans= ans - (long)((long)i*(long)a[i+1]);
}
log.print(ans+"\n");
}
log.close();
}
}
class OutputWriter
{
BufferedWriter writer;
public OutputWriter(OutputStream stream){
writer = new BufferedWriter(new OutputStreamWriter(stream));
}
public void print(int i) throws IOException {
writer.write(i);
}
public void print(String s) throws IOException {
writer.write(s);
}
public void print(char []c) throws IOException {
writer.write(c);
}
public void close() throws IOException {
writer.close();
}
}
class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
}
else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0,
BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 0bd0a53179f3fbd4e6c12ed33d8157a3 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
public class q3 {
public static class Edge {
int v;
int wt;
Edge(int v, int wt) {
this.v = v;
this.wt = wt;
}
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tests = Integer.parseInt(br.readLine());
for (int test = 1; test <= tests; test++) {
String[] parts = br.readLine().split(" ");
int n = Integer.parseInt(parts[0]);
parts = br.readLine().split(" ");
if(n == 1 || n == 2) System.out.println(0);
else{
long ans = 0;
Long[] arr = new Long[n];
for(int i = 0;i < n;i++){
arr[i] = Long.parseLong(parts[i]);
}
Arrays.sort(arr);
for(int i = n - 1;i > 0;i--){
arr[i] = arr[i] - arr[i - 1];
ans += arr[i];
ans = ans - (arr[i] * (n - i) * i);
}
System.out.println(ans);
}
}
}
// public static long Prims(ArrayList<Edge>[] graph){
// }
public static long[] mergeSort(long[] arr, int lo, int hi) {
if (lo == hi) {
long[] base = new long[1];
base[0] = arr[lo];
return base;
}
int mid = (lo + hi) / 2;
long[] a = mergeSort(arr, lo, mid);
long[] b = mergeSort(arr, mid + 1, hi);
return mergeTwoSortedArrays(a, b);
}
public static long[] mergeTwoSortedArrays(long[] a, long[] b) {
int i = 0, j = 0, k = 0;
long[] ans = new long[a.length + b.length];
while (i < a.length && j < b.length) {
if (a[i] <= b[j]) {
ans[k] = a[i];
i++;
k++;
} else {
ans[k] = b[j];
j++;
k++;
}
}
while (i < a.length) {
ans[k] = a[i];
k++;
i++;
}
while (j < b.length) {
ans[k] = b[j];
k++;
j++;
}
return ans;
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 6f2208815f839d31119975d15930e383 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class C implements Runnable {
public static void main(String[] args) throws Exception {
new Thread(null, new C(), "SwapnilGanguly", 1 << 24).start();
}
public void run() {
MyScanner sc = new MyScanner();
PrintWriter pr = new PrintWriter(System.out);
int T = sc.nextInt();
while (T-- > 0) {
int n = sc.nextInt();
long[] ar = new long[n];
for (int i = 0; i < n; i++)
ar[i] = sc.nextLong();
if (n <= 2) {
pr.println(0);
continue;
}
sort(ar);
long[] suffix = new long[n];
suffix[n - 1] = ar[n - 1];
for (int i = n - 2; i >= 0; i--)
suffix[i] = ar[i] + suffix[i + 1];
long ans = 0L;
for (int i = 1; i < n - 2; i++) {
long sum = suffix[i + 2];
long tot = sum - ((n - 2 - i) * ar[i]);
ans += tot;
// System.out.println(tot);
}
pr.println(-(ans + suffix[2]));
}
pr.close();
}
public static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] ar = new int[n];
for (int i = 0; i < n; i++)
ar[i] = nextInt();
return ar;
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | e424c88afd08961e2cb80f7568b1924c | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
{
int n=s.nextInt();
long a[]=new long[n];
for(int i=0;i<n;i++)
a[i]=s.nextLong();
Arrays.sort(a);
long ans=0L,temp=0L;
for(int i=2;i<n;i++)
{
temp=temp+a[i-2];
ans=ans-(a[i]*(i-1));
ans=ans+temp;
}
System.out.println(ans);
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | e5c96baab6828e9b7e33fdaab4ea28e9 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.io.*;
import java.util.*;
public class prob3
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine().trim());
int t=Integer.parseInt(st.nextToken());
for(int e=1;e<=t;e++)
{
int n=Integer.parseInt(br.readLine());
st=new StringTokenizer(br.readLine());
int[] arr=new int[n];
long sum=0;
for(int i=0;i<n;i++)
arr[i]=Integer.parseInt(st.nextToken());
Arrays.sort(arr);
int[] diff=new int[n];
for(int i=1;i<n;i++)
diff[i]=arr[i]-arr[i-1];
for(int i=1;i<n;i++)
sum+=(long)diff[i]*((n-i)*(long)(i));
long ans=arr[n-1]-sum;
System.out.println(ans);
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | ca4717c67441037b8edd4aedaaaa84e8 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | import java.util.*;
public class C{
static void sort(long[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
long mod=1000000007l;
int cases=sc.nextInt();
while(cases>0)
{
cases--;
int n=sc.nextInt();
long arr[]=new long[n];
for(int j=0;j<n;j++)
{
arr[j]=sc.nextLong();
}
long ans=0;
sort(arr);
for(int j=0;j<n-2;j++)
{
ans=ans-((long)(n-2-j))*arr[j]+((long)(n-2-j))*arr[n-1-j];
}
// for(int j=0;j<n-2;j++)
// {
// ans=ans+((long)(n-2-j))*arr[n-1-j];
// }
System.out.println(-ans);
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | 01519cfe3d98603e61fd6eae91f8a9d4 | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes |
// package com.vikhyat;
import java.io.*;
import java.util.*;
public class Main {
//source: gfg
static void merge(long arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long[n1];
long R[] = new long[n2];
/*Copy data to temp arrays*/
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
static void sort(long arr[], int l, int r)
{
if (l < r) {
// Find the middle point
int m =l+ (r-l)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
public static void swap1(int i, int j, long[] arr){
long temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
public static void main(String[] Args) throws IOException {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
long arr[]=new long[n];
long sum[]=new long[n];
for (int i = 0; i < n; i++) {
arr[i]=sc.nextInt();
}
if(n<=2){
System.out.println(0);
continue;
}
sort(arr,0,n-1);
for (int i = 1; i <n ; i++) {
sum[i]=sum[i-1]+arr[i];
}
long sum1=0;
for (int i = 2; i <n ; i++) {
sum1+=-1*(i-1)*arr[i] +(sum[i-2]);
}
System.out.println(sum1);
}
}
}
| Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output | |
PASSED | e3909d18a855a183d01956b2e2fe600f | train_107.jsonl | 1624635300 | Farmer John has a farm that consists of $$$n$$$ pastures connected by one-directional roads. Each road has a weight, representing the time it takes to go from the start to the end of the road. The roads could have negative weight, where the cows go so fast that they go back in time! However, Farmer John guarantees that it is impossible for the cows to get stuck in a time loop, where they can infinitely go back in time by traveling across a sequence of roads. Also, each pair of pastures is connected by at most one road in each direction.Unfortunately, Farmer John lost the map of the farm. All he remembers is an array $$$d$$$, where $$$d_i$$$ is the smallest amount of time it took the cows to reach the $$$i$$$-th pasture from pasture $$$1$$$ using a sequence of roads. The cost of his farm is the sum of the weights of each of the roads, and Farmer John needs to know the minimal cost of a farm that is consistent with his memory. | 256 megabytes | // don't place package name class name "Codechef", Main only if class is public
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.*;
public final class Main{
public static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br=new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st==null || !st.hasMoreElements()){
try{
st=new StringTokenizer(br.readLine());
}catch (Exception e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
float nextFloat(){
return Float.parseFloat(next());
}
String nextLine(){
String str="";
try{
str=br.readLine();
}catch (Exception e){
e.printStackTrace();
}
return str;
}
}
static FastReader in= new FastReader();
public static void main(String[] args) throws FileNotFoundException {
long t= in.nextLong();
while (t-->0){
solve();
}
}
static void solve(){
int n= in.nextInt();
long[] arr= new long[n];
for(int i=0;i<n;i++){
arr[i]= in.nextLong();
}
Arrays.sort(arr);
long ans=0, sum=0;
for(int i=2;i<n;i++){
sum += arr[i-2];
ans -= (arr[i] * (i-1));
ans += sum;
}
System.out.println(ans);
}
static int MAX= 1000000;
static int[] precomp= new int[MAX+1];
static void sieveOfEratosthenes(){
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
int n= MAX;
boolean prime[] = new boolean[(n+1)];
for(int i=0;i<=n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
// If prime[p] is not changed, then it is a prime
if(prime[p] == true)
{
// Update all multiples of p
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for(int i = 2; i <= n; i++)
{
if(prime[i] == true)
precomp[i]= 1;
}
}
int isDigitSumPalindrome(int N) {
// code here
int sum= sumOfDigits(String.valueOf(N));
int rev=0;
int org= sum;
while (sum!=0){
int d= sum%10;
rev = rev*10 +d;
sum /= 10;
}
if (org==rev){
return 1;
}else{
return 0;
}
}
int sumOfDigits(String n){
int sum= 0;
for (char c: n.toCharArray()){
sum += Integer.parseInt(String.valueOf(c));
}
return sum;
}
public static int[] revArray(int[] arr) {
int n= arr.length;
int i=0, j=n-1;
while (i<j){
int temp= arr[i];
arr[i]= arr[j];
arr[j]= temp;
i++;
j--;
}
return arr;
}
static long gcd(long a, long b){
if (b==0)
return a;
return gcd(b, a%b);
}
static long lcm(long a,long b){
return (a*b)/gcd(a,b);
}
static class Pair{
int first;
int second;
Pair(int x,int y){
this.first= x;
this.second= y;
}
}
static class Compare {
static void compare(ArrayList<Pair> arr, long n) {
arr.sort(new Comparator<Pair>() {
@Override
public int compare(Pair p1, Pair p2) {
return (int) (p1.first - p2.first);
}
});
}
}
} | Java | ["3\n3\n0 2 3\n2\n0 1000000000\n1\n0"] | 2 seconds | ["-3\n0\n0"] | NoteIn the first test case, you can add roads from pasture $$$1$$$ to pasture $$$2$$$ with a time of $$$2$$$, from pasture $$$2$$$ to pasture $$$3$$$ with a time of $$$1$$$, from pasture $$$3$$$ to pasture $$$1$$$ with a time of $$$-3$$$, from pasture $$$3$$$ to pasture $$$2$$$ with a time of $$$-1$$$, from pasture $$$2$$$ to pasture $$$1$$$ with a time of $$$-2$$$. The total cost is $$$2 + 1 + -3 + -1 + -2 = -3$$$.In the second test case, you can add a road from pasture $$$1$$$ to pasture $$$2$$$ with cost $$$1000000000$$$ and a road from pasture $$$2$$$ to pasture $$$1$$$ with cost $$$-1000000000$$$. The total cost is $$$1000000000 + -1000000000 = 0$$$.In the third test case, you can't add any roads. The total cost is $$$0$$$. | Java 11 | standard input | [
"constructive algorithms",
"graphs",
"greedy",
"shortest paths",
"sortings"
] | 7dfe0db5a99e6e4d71eb012fab07685b | The first line contains one integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of pastures. The second line of each test case contains $$$n$$$ space separated integers $$$d_1, d_2, \ldots, d_n$$$ ($$$0 \leq d_i \leq 10^9$$$) — the array $$$d$$$. It is guaranteed that $$$d_1 = 0$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. | 1,400 | For each test case, output the minimum possible cost of a farm that is consistent with Farmer John's memory. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.