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.io.*;
import java.util.*;
public class fancyfence
{
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
String next() { // reads in the next string
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() { // reads in the next int
return Integer.parseInt(next());
}
public long nextLong() { // reads in the next long
return Long.parseLong(next());
}
public double nextDouble() { // reads in the next double
return Double.parseDouble(next());
}
}
static long m = 1000000007;
static InputReader r = new InputReader(System.in);
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args)
{
int n = r.nextInt();
Stack<Integer> stack = new Stack<Integer>();
long[] h = new long[n+1]; long[] w = new long[n+1]; long[] total = new long[n+1]; long tot = 0;
for (int i = 1; i <= n; i ++)
{
h[i] = r.nextLong();
}
for (int i = 1; i <= n; i ++)
{
w[i] = r.nextLong();
}
long ans = 0;
stack.add(0);
for (int i = 1; i <= n; i ++)
{
long remove = 0;
while (h[stack.peek()] >= h[i])
{
int j = stack.pop();
remove += w[j]; remove%=m;
}
long x = ((h[i])*(h[i]+1))/2; x%=m; long y = ((w[i])*(w[i]+1))/2; y%=m;
tot = total[stack.peek()] + (remove*x)%m;
tot%=m;
ans += (tot*w[i])%m; ans%=m;
ans += (x*y)%m; ans%=m;
total[i] = tot + (w[i]*x)%m; total[i]%=m;
w[i] += remove; w[i]%=m;
stack.add(i);
}
pw.println(ans);
pw.close();
}
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |