Problem:
Score : 200 points

Problem Statement

It's now the season of TAKOYAKI FESTIVAL!

This year, N takoyaki (a ball-shaped food with a piece of octopus inside) will be served. The deliciousness of the i-th takoyaki is d_i.

As is commonly known, when you eat two takoyaki of deliciousness x and y together, you restore x \times y health points.

There are \frac{N \times (N - 1)}{2} ways to choose two from the N takoyaki served in the festival. For each of these choices, find the health points restored from eating the two takoyaki, then compute the sum of these \frac{N \times (N - 1)}{2} values.

Constraints

All values in input are integers.

2 \leq N \leq 50

0 \leq d_i \leq 100

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the sum of the health points restored from eating two takoyaki over all possible choices of two takoyaki from the N takoyaki served.

Sample Input 1

3
3 1 2

Sample Output 1

11

There are three possible choices:

Eat the first and second takoyaki. You will restore 3 health points.

Eat the second and third takoyaki. You will restore 2 health points.

Eat the first and third takoyaki. You will restore 6 health points.

The sum of these values is 11.

Sample Input 2

7
5 0 7 8 3 3 2

Sample Output 2

312

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::io::BufRead;
use std::str::FromStr;

fn main() {
    let stdin = std::io::stdin();
    let mut t = Tokenizer::new(stdin.lock().lines().map(|x| x.unwrap()));

    let n: i32 = t.take();
    let mut d: Vec<i32> = Vec::new();
    for _i in 0..n {
        d.push( t.take() );
    }
    let ans = problem( n,d );
    println!("{}",ans);
}

fn problem( n: i32, d: Vec<i32> ) -> i64 {
    let mut sum: i64 = 0;
    for i in 0..n {
        for j in i+1..n {
            if i == n {
                continue;
            }
            sum += (d[i as usize]*d[j as usize]) as i64;
        }
    }
    sum
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn test1() {
        let result = problem( 3, vec![3,1,2] );
        assert_eq!(result,11);
    }
    #[test]
    fn test2() {
        let result = problem( 7, vec![5,0,7,8,3,3,2] );
        assert_eq!(result,312);
    }
}


struct Tokenizer<S: Iterator<Item = String>> {
    input: S,
    items: Vec<String>,
}

impl<S: Iterator<Item = String>> Tokenizer<S> {
    pub fn new(input: S) -> Self {
        Tokenizer {
            input: input,
            items: Vec::new(),
        }
    }

    pub fn take<T: FromStr>(&mut self) -> T {
        loop {
            if let Some(r) = self.items.pop() {
                return r.parse().ok().expect("parse failed");
            }
            let mut items = self
                .input
                .next()
                .unwrap()
                .split_whitespace()
                .map(|x| x.to_string())
                .collect::<Vec<String>>();
            items.reverse();
            self.items = items;
        }
    }
}