This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import java.util.*;
import java.io.*;
public class art {
public static void main(String[] args) throws IOException{
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new PrintStream(System.out));
int n=Integer.parseInt(f.readLine());
long ans=0;
// Integer[]bob=new Integer[400];
// Arrays.sort(bob);
pair[]arr=new pair[n];
for(int i=0;i<n;i++){
StringTokenizer st=new StringTokenizer(f.readLine());
long a=Long.parseLong(st.nextToken());
long b=Long.parseLong(st.nextToken());
ans=Math.max(ans,b);
arr[i]=new pair(a,b);
}
sort(arr,0,n-1);
long best=0;
long pfx=0;
long lastpfx=0;
for(int i=0;i<n;i++){
pfx+=arr[i].val;
ans=Math.max(pfx-arr[i].size+best,ans);
best=Math.max(best,-lastpfx+arr[i].size);
lastpfx=pfx;
}
System.out.print(ans);
f.close();
out.close();
}
static void merge(pair 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 */
pair L[] = new pair [n1];
pair R[] = new pair [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].size <= R[j].size)
{
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(pair arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
}class pair implements Comparable <pair>{
Long size;
Long val;
public int compareTo(pair other){
if(size>other.size)return 1;
return -1;
}
pair(long a, long b)
{
size=a;
val=b;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |