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. cordova常用资料源

    cordova的中文资料https://github.com/CordovaCn/CordovaCn 官方文档: http://cordova.apache.org/docs/en/latest/ E ...

  2. Regex

    1. regex with variable example: find the number and put a parenthese around the number. output: a(52 ...

  3. Python——函数的命名关键字参数

    命名关键字参数 对于关键字参数,函数的调用者可以传入任意不受限制的关键字参数.至于到底传入了哪些,就需要在函数内部通过kw检查. 仍以person()函数为例,我们希望检查是否有city和job参数: ...

  4. Ansible学习笔记

    一.Ansible简介 Ansible是一种agentless(基于ssh),可实现批量配置.命令执行和控制,基于Python实现的自动化运维工具. 其特性有: ①模块化:通过调用相关模块,完成指定任 ...

  5. CSS 图片倾斜的制作

    <style> #zhong{ height:600px; width:1350px; position:relative; z-index:2} .znei{ height:60px; ...

  6. 关于c#静态构造函数

    http://baike.baidu.com/view/2634573.htm?fr=aladdin 在百科上看到C#的新特性静态构造函数,其中提到静态构造函数“不能继承” 今天做了个试验,发现实际上 ...

  7. flask文件的上传和下载

    from werkzeug.utils import secure_filename from flask import Flask,render_template,jsonify,request i ...

  8. mina IoBuffer

    mina IoBuffer 常用方法   Limit(int) 如果position>limit, position = limit,如果mark>limit, 重置mark Mark() ...

  9. Java线程池入门

    序 为什么要用线程池?什么情况下才会用到线程池? 并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁创建线程和销毁线程需要时间. 因此 ...

  10. jQuery将悬停效果加到菜单项

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...