% https://open.kattis.com/problems/fluortanten % Time limit: 3s % Difficulty: 1.2-2.4 Easy % Picat for some explorations.... import util. main :- % Ns = read_file_lines().tail.first.split().map(to_int), % Ns = 1..5, % Ns = [1,-2], Ns = [1,3,19,212], println(Ns), member(Ix,1..Ns.len+1), println(ix=Ix), Ns2=insert(Ns,Ix,0), println(ns2=Ns2=scalar_product2(1..Ns2.len,Ns2)), L = [], foreach({I,N} in zip(1..Ns2.len,Ns2)) L := L ++ [I*N], % println(l=L=L.sum) end, println(sum=L.sum), nl, fail, nl. main. main2 :- % Ns = [0,1,-2], Ns = [0,1,3,19,212], println(Ns), scalar_product2(1..Ns.len,Ns) = Sum, t(Ns,1,Sum,0,Max), println(max=Max), nl. % If the sequence is increasing then this seems to work, % but not for decreasing or "un-creasing" sequences. % But it's because the first sequence yields the max value... % t([],_I,_Sum,M,M). t([N|Ns],I,Sum,M0,M) :- T is Sum-I*N, println(t=T), M1 is max(M0,Sum-I*N), println(m1=M1), I1 is I+1, t(Ns,I1,Sum,M1,M). scalar_product2(L,X) = Sum => scalar_product2(L,X,0,Sum). scalar_product2([],[],S,S). scalar_product2([L|Ls],[X|Xs],M0,M) :- M1 is M0 + L*X, scalar_product2(Ls,Xs,M1,M).