Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
//Прямой перебор
function alg(array $arr) : int
{
$sum = 0;
$j = 0;
$count = 0;
while($count <= count($arr) - 1 ){
$max = 0;
for ($i = $j; $i <= count($arr) - 1; $i++ ){
if($arr[$i] > $max){
$max = $arr[$i];
$j = $i;
}
}
$sum = $max * ($j + 1 - $count);
$count = $i;
}
return end($arr);
}
$arr = [1, 3, 1, 2];
echo alg($arr);