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. One or more types required to compile a dynamic expression cannot be found.

    This is because dynamic keyword is a new C# keyword. So we need to import Microsoft.CSharp.dll. Here ...

  2. nginx配置文件httpd.conf详解

     PS:Nginx使用有两三年了,现在经常碰到有新用户问一些很基本的问题,我也没时间一一回答,今天下午花了点时间,结合自己的使用经验,把Nginx的主要配置参数说明分享一下,也参考了一些网络的内容,这 ...

  3. secure boot(安全启动)下为内核模块签名

    上一篇随笔中提到了如何在secure boot下安装Nvidia显卡驱动 >>上一篇随笔 如果不需要安装Nvidia显卡驱动,而且要生成密钥,可以参考>> 这篇文章 这里假设生 ...

  4. quartz_job

    quartz--job job是执行任务所需的Java类. 在quartz中job分为两种,有状态和无状态的.job是无状态的,statefuljob是有状态的. 通过实现job或者statefulj ...

  5. ajax请求封装

    var xmlHttp; var ajaxRequest = function (params) { xmlHttp ={}; this.url = params.url; this.method = ...

  6. EF测试自动修改数据库结构

    1.配置类 <connectionStrings> <add name="test1" connectionString="Data Source=.; ...

  7. Oracle RMAN 恢复控制文件到指定的路径

    Oracle 数据库通过RMAN恢复控制文件到指定的路径 --------------------------------------------------------- 先查询备份集信息,再指定备 ...

  8. Nginx-->基础-->排错-->nginx错误总结

    一.启动时错误 1.错误提示: 2016/11/16 17:36:41 [emerg] 2458#2458: getpwnam("nginx") failed 查看错误日志文件内容 ...

  9. zabbix nagios 类nagios 之 不以性能为前提的开发和监控都是瞎扯淡

    从最初的nagios到现在强大的zabbix 3.0,我想说,不以性能为前提的开发和监控都是瞎扯淡? 首先我对这两款监控软件的认识: zabbix,很多企业都在用,给人的感觉是很炫,不过我个人觉得虽然 ...

  10. 笔记本win8,mac10.10,ubuntu,android四系统安装

    前言,最简单的是win8和ubuntu 最难啃的是mac android版只是要注意一个小技巧,目前算是独创 (被android坑了一次,两块硬件,android版把500Gntfs的硬盘整个识别为一 ...