码迷,mamicode.com
首页 > 其他好文 > 详细

Maximum Pairwise Product

时间:2016-05-17 22:33:16      阅读:510      评论:0      收藏:0      [点我收藏+]

标签:

问题描述

Given a sequence of non-negative integers a0,…,an−1, find the maximum pairwise product, that is, the largest integer that can be obtained by multiplying two different elements from the sequence (or, more formally, max0≤i≠j≤n−1aiaj). Different elements here mean ai and ajwith i≠j (it can be the case that ai=aj).

输入格式

The first line of the input contains an integer n. The next line contains nnon-negative integers a0,…,an−1 (separated by spaces).

限制条件

2≤n≤2⋅105; 0≤a0,…,an−1≤105.

输出格式

Output a single number — the maximum pairwise product.

 

样例 1

输入:

3

1 2 3

输出:

6

 

样例 2

输入:

10

7 5 14 2 8 8 10 1 2 3

输出:

140

 

样例 3 

输入:

5

4 6 2 6 1

输出:

36

 

 

n = int(input())
a = [int(x) for x in input().split()]
assert(len(a) == n)

curMax=sndMax=0
for idx in range(0, n):
    if curMax < a[idx]:
        sndMax = curMax
        curMax = a[idx]
    elif sndMax < a[idx]:
        sndMax=a[idx]
print(curMax*sndMax)

Maximum Pairwise Product

标签:

原文地址:http://www.cnblogs.com/dm1299/p/5503315.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!