The weighted graph

3.4

9 votes
Algorithms, Basics of Greedy Algorithms, C++, Greedy Algorithms
Problem

You are given a complete graph with n nodes. There are two values associated with each node i, denoted by ai and bi.

The weight of the edge between two nodes i and j is denoted by wij=|aibjajbi|.

Find the sum of the square of weights of all the edges. Formally, find the value ni=1nj=i+1w2ij.

Input format

  • The first line contains a single integer n.
  • The second line contains n space-separated integers a1,a2,...,an.
  • The third line contains n space-separated integers b1,b2,...,bn.

Output format

Print a single integer denoting the required value (mod 109+7).

Constraints

2n1061015ai,bi1015

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

S=w212+w213+w223=32+12+32=19

Editor Image

?