#import <stdio.h>
#import <limits.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
typedef long long INT;
int main(){
    int n,a;
    scanf("%d",&n);
    int S=0,T=0;
    int min = INT_MAX;
    int max = -INT_MAX;
    for(int i=0;i<n;i++){
        scanf("%d",&a);
        S+=a*a;
        T+=a;
        min=MIN(min,a);
        max=MAX(max,a);
    }
    INT result=LONG_MAX;
    for(int x=min;x<=max;x++){
        result=MIN(result,S-2*x*T+x*x*n);
    }
    printf("%lld",result);
}

; ModuleID = '/data/TheStack_IR/OJ_Samples/Collated/ObjectiveC/Source_922/source.m'
source_filename = "/data/TheStack_IR/OJ_Samples/Collated/ObjectiveC/Source_922/source.m"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

@.str = private unnamed_addr constant [3 x i8] c"%d\00", align 1
@.str.1 = private unnamed_addr constant [5 x i8] c"%lld\00", align 1

; Function Attrs: nofree nounwind uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
entry:
  %n = alloca i32, align 4
  %a = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %n) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %a) #4
  %call = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %n)
  %0 = load i32, ptr %n, align 4, !tbaa !5
  %cmp53 = icmp sgt i32 %0, 0
  br i1 %cmp53, label %for.body, label %for.cond.cleanup11

for.cond9.preheader:                              ; preds = %for.body
  %1 = shl i32 %add2, 1
  %2 = add i32 %cond8, 1
  %3 = sub i32 %2, %cond
  %xtraiter = and i32 %3, 1
  %4 = icmp eq i32 %cond8, %cond
  br i1 %4, label %for.cond.cleanup11.loopexit.unr-lcssa, label %for.cond9.preheader.new

for.cond9.preheader.new:                          ; preds = %for.cond9.preheader
  %5 = zext i32 %cond to i64
  %unroll_iter = and i32 %3, -2
  br label %for.body12

for.body:                                         ; preds = %entry, %for.body
  %i.058 = phi i32 [ %inc, %for.body ], [ 0, %entry ]
  %max.057 = phi i32 [ %cond8, %for.body ], [ -2147483647, %entry ]
  %min.056 = phi i32 [ %cond, %for.body ], [ 2147483647, %entry ]
  %T.055 = phi i32 [ %add2, %for.body ], [ 0, %entry ]
  %S.054 = phi i32 [ %add, %for.body ], [ 0, %entry ]
  %call1 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str, ptr noundef nonnull %a)
  %6 = load i32, ptr %a, align 4
  %mul = mul nsw i32 %6, %6
  %add = add nuw nsw i32 %mul, %S.054
  %add2 = add nsw i32 %6, %T.055
  %cond = call i32 @llvm.smin.i32(i32 %min.056, i32 %6)
  %cond8 = call i32 @llvm.smax.i32(i32 %max.057, i32 %6)
  %inc = add nuw nsw i32 %i.058, 1
  %7 = load i32, ptr %n, align 4, !tbaa !5
  %cmp = icmp slt i32 %inc, %7
  br i1 %cmp, label %for.body, label %for.cond9.preheader

for.cond.cleanup11.loopexit.unr-lcssa.loopexit:   ; preds = %for.body12
  %8 = trunc i64 %indvars.iv.next.1 to i32
  br label %for.cond.cleanup11.loopexit.unr-lcssa

for.cond.cleanup11.loopexit.unr-lcssa:            ; preds = %for.cond.cleanup11.loopexit.unr-lcssa.loopexit, %for.cond9.preheader
  %result.0.conv.lcssa.ph = phi i64 [ undef, %for.cond9.preheader ], [ %result.0.conv.1, %for.cond.cleanup11.loopexit.unr-lcssa.loopexit ]
  %indvars.iv.unr = phi i32 [ %cond, %for.cond9.preheader ], [ %8, %for.cond.cleanup11.loopexit.unr-lcssa.loopexit ]
  %result.064.unr = phi i64 [ 9223372036854775807, %for.cond9.preheader ], [ %result.0.conv.1, %for.cond.cleanup11.loopexit.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %for.cond.cleanup11, label %for.body12.epil

for.body12.epil:                                  ; preds = %for.cond.cleanup11.loopexit.unr-lcssa
  %mul16.epil = mul i32 %7, %indvars.iv.unr
  %reass.add.epil = sub i32 %mul16.epil, %1
  %reass.mul.epil = mul i32 %reass.add.epil, %indvars.iv.unr
  %add17.epil = add i32 %reass.mul.epil, %add
  %conv.epil = sext i32 %add17.epil to i64
  %result.0.conv.epil = call i64 @llvm.smin.i64(i64 %result.064.unr, i64 %conv.epil)
  br label %for.cond.cleanup11

for.cond.cleanup11:                               ; preds = %for.body12.epil, %for.cond.cleanup11.loopexit.unr-lcssa, %entry
  %result.0.lcssa = phi i64 [ 9223372036854775807, %entry ], [ %result.0.conv.lcssa.ph, %for.cond.cleanup11.loopexit.unr-lcssa ], [ %result.0.conv.epil, %for.body12.epil ]
  %call34 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1, i64 noundef %result.0.lcssa)
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %a) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %n) #4
  ret i32 0

for.body12:                                       ; preds = %for.body12, %for.cond9.preheader.new
  %indvars.iv = phi i64 [ %5, %for.cond9.preheader.new ], [ %indvars.iv.next.1, %for.body12 ]
  %result.064 = phi i64 [ 9223372036854775807, %for.cond9.preheader.new ], [ %result.0.conv.1, %for.body12 ]
  %niter = phi i32 [ 0, %for.cond9.preheader.new ], [ %niter.next.1, %for.body12 ]
  %9 = trunc i64 %indvars.iv to i32
  %mul16 = mul i32 %7, %9
  %reass.add = sub i32 %mul16, %1
  %reass.mul = mul i32 %reass.add, %9
  %add17 = add i32 %reass.mul, %add
  %conv = sext i32 %add17 to i64
  %result.0.conv = call i64 @llvm.smin.i64(i64 %result.064, i64 %conv)
  %10 = trunc i64 %indvars.iv to i32
  %11 = add i32 %10, 1
  %mul16.1 = mul i32 %7, %11
  %reass.add.1 = sub i32 %mul16.1, %1
  %reass.mul.1 = mul i32 %reass.add.1, %11
  %add17.1 = add i32 %reass.mul.1, %add
  %conv.1 = sext i32 %add17.1 to i64
  %result.0.conv.1 = call i64 @llvm.smin.i64(i64 %result.0.conv, i64 %conv.1)
  %indvars.iv.next.1 = add i64 %indvars.iv, 2
  %niter.next.1 = add i32 %niter, 2
  %niter.ncmp.1 = icmp eq i32 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %for.cond.cleanup11.loopexit.unr-lcssa.loopexit, label %for.body12
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nofree nounwind
declare noundef i32 @__isoc99_scanf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smin.i32(i32, i32) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #3

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.smin.i64(i64, i64) #3

attributes #0 = { nofree nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nofree nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #4 = { nounwind }

!llvm.linker.options = !{}
!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"Ubuntu clang version 18.0.0 (++20231007042255+8abb2ace888b-1~exp1~20231007042414.1230)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
