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. 设置mysql utf8

    1.修改mysql 配置文件my.cnf 标签[mysqld]下添加即可 character-set-server = utf8

  2. MFC对话框中显示BMP,JPG图片

    //************************************ // 方法说明:    显示JPG和GIF.BMP图片 // 参数说明:    CDC * pDC           设 ...

  3. Oracle中的sql操作符 和分析函数

    Oracle中的操作符算术操作符:无论是在sqlserver,或者是java中,每种语言它都有算术操作符,大同小异. Oracle中算术操作符(+)(-)(*)(/) 值得注意的是:/ 在oracle ...

  4. 【转】MySQL连接超时断开的问题

      这遍文章转过来做个笔记,时不时看看. 转:http://blog.csdn.net/nethibernate/article/details/6658855 Exception如下: org.hi ...

  5. ajaxSubmit

    $('button').on('click', function() {    $('form').on('submit', function() {        var title = $('in ...

  6. 如何在xml文件中加注释或取消注释

    1)加注释:Ctrl+Shift+/ 2)取消注释:Ctrl+Shift+\ 本文欢迎转载,但请注明作者与出处: 作者:Sophia 出处:http://www.cnblogs.com/Sophia- ...

  7. linux学习之——vim简明教程

    摘自  http://blog.csdn.net/niushuai666/article/details/7275406 ——————————正文开始—————————— 你想以最快的速度学习人类史上 ...

  8. asp.net web api返回图片至前端

    var response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new ByteArrayContent(da ...

  9. Struts 2学习笔记——拦截器相关

    一.添加国际化支持 默认的struts-deault.xml文件中已经定义了国际化拦截器,内容如下 <!-定义国际化拦截器--> <interceptor name="i1 ...

  10. ORA-12514 TNS:listener does not currently know of service requested in connect descriptor

    this issue and the fix was to make sure in tnsnames.ora the SERVICE_NAME is a valid service name in ...