Created
May 6, 2020 08:22
-
-
Save SyureNyanko/91e37d266bf7608b87e34f2313fb5607 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"strconv" | |
) | |
func main() { | |
var scanner = bufio.NewScanner(os.Stdin) | |
scanner.Split(bufio.ScanWords) | |
var N int | |
if scanner.Scan() { | |
N, _ = strconv.Atoi(scanner.Text()) | |
} | |
A := make([]int, N) | |
var sum int | |
for i := 0; i < N; i++ { | |
if scanner.Scan() { | |
A[i], _ = strconv.Atoi(scanner.Text()) | |
sum += A[i] | |
} | |
} | |
x := sum / N | |
var ans1 int | |
for _, v := range A { | |
ans1 += (v - x) * (v - x) | |
} | |
var ans2 int | |
x++ | |
for _, v := range A { | |
ans2 += (v - x) * (v - x) | |
} | |
var ans int | |
if ans1 < ans2 { | |
ans = ans1 | |
} else { | |
ans = ans2 | |
} | |
var wtr = bufio.NewWriter(os.Stdout) | |
fmt.Fprintln(wtr, ans) | |
_ = wtr.Flush() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment