Problem Description

Task.Given a sequence of non-negative integers \(a_0, ..., a_{n-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,\(\max \limits_{0\leq{i \neq j}\leq {n-1}}\ a_ia_j\)).Different elements here mean \(a_i\) and \(a_j\) with \(i \neq j\) (it can be the case that \(a_i=a_j\)).

Input format.The first line of the input contains an integer \(n\).The next line contains\(n\)non-negative integers \(a_0, ..., a_{n-1}\) (separated by spaces).

Constraints.\(2 \leq n \leq 2 \cdot 10^5; 0 \leq a_0, ..., a_{n-1} \leq 10^5\).

Output format.Output a single number - the maximum pairwise product.

Sample 1.
Input:

3
1 2 3

Output:

6

Sample 2.
Input:

10
7 5 14 2 8 8 10 1 2 3

Output:

140

Sample 3.
Input:

5
4 6 2 6 1

Output:

36

Solution

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

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

[UCSD白板题] Maximum Pairwise Product的更多相关文章

  1. [UCSD白板题] Minimum Dot Product

    Problem Introduction The dot product of two sequences \(a_1,a_2,\cdots,a_n\) and \(b_1,b_2,\cdots,b_ ...

  2. [UCSD白板题] Pairwise Distinct Summands

    Problem Introduction This is an example of a problem where a subproblem of the corresponding greedy ...

  3. [UCSD白板题] Maximize the Value of an Arithmetic Expression

    Problem Introduction In the problem, your goal is to add parentheses to a given arithmetic expressio ...

  4. [UCSD白板题] Take as Much Gold as Possible

    Problem Introduction This problem is about implementing an algorithm for the knapsack without repeti ...

  5. [UCSD白板题] Binary Search

    Problem Introduction In this problem, you will implemented the binary search algorithm that allows s ...

  6. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  7. [UCSD白板题] Compute the Edit Distance Between Two Strings

    Problem Introduction The edit distinct between two strings is the minimum number of insertions, dele ...

  8. [UCSD白板题] Primitive Calculator

    Problem Introduction You are given a primitive calculator that can perform the following three opera ...

  9. [UCSD白板题] Points and Segments

    Problem Introduction The goal in this problem is given a set of segments on a line and a set of poin ...

随机推荐

  1. error-2016-2-15

    错误:该请求包含双重转义序列,而 Web 服务器上配置的请求筛选拒绝双重转义序列原因:一些URL中可能会包含+号等符号,然后IIS7以上的版本会默认拒绝请求此URL,需要进行如下的修改. 解决PHP中 ...

  2. LeetCode 214 Shortest Palindrome

    214-Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding ch ...

  3. lua 学习笔记一

    参考: http://www.kancloud.cn/digest/luanote/119924 1.基础概念 1.lua八种基本数据类型:nil.boolean.string.function.ta ...

  4. hdu5452 Minimum Cut

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5452 题意:给你一个图和它的生成树,要你在树上删一条边,问你最少删多少条边使得图不联通(开始时图一定联 ...

  5. 驱动开发学习笔记. 0.06 嵌入式linux视频开发之预备知识

    驱动开发读书笔记. 0.06  嵌入式linux视频开发之预备知识 由于毕业设计选择了嵌入式linux视频开发相关的项目,于是找了相关的资料,下面是一下预备知识 UVC : UVC,全称为:USB v ...

  6. Xcode8 安装插件

    关闭Xcode 一.进入https://github.com/inket/update_xcode_plugins下载 二.打开终端,输入sudo gem install update_xcode_p ...

  7. C#中的?和??的用法

    1. 可空类型修饰符(?):引用类型可以使用空引用表示一个不存在的值,而值类型通常不能表示为空.例如:string str=null; 是正确的,int i=null; 编译器就会报错.为了使值类型也 ...

  8. alphaRGB 转 RGB、16位

    struct xColor { BYTE b, g, r, a; }; struct RGBColor { BYTE b, g, r; }; //void operator <<(RGBC ...

  9. Flash Builder 调试器无法连接到正在运行的应用程序(57%)

    Flash Builder 调试器无法连接到正在运行的应用程序(57%),可能原因:     1,flashplayer不是debug版.     2,调试器(用debug版flashplayer随便 ...

  10. 3.使用git提交项目到开源中国(gitosc)

    1.提交地址 使用的是开源中国git仓库 git.oschina.net 在windos环境下使用msysgit. 2.初始化化 username.email初始化 git config --glob ...