Is a 3.9-4.0 unweighted GPA necessary for the top schools?

Last year (freshman year) I got a b second semester in honors chemistry, which I saw coming because I hate chemistry and had no motivation in that class towards the end of the year to do well, but this year I might end up with a b first semester in honors advanced precalc (called honors advanced cause college board doesn’t have AP precalc.) I slacked off at the beginning of the semester so my grade is currently at an 84 and we only have a quiz and the final before the end of the semester. It is possible for me to bring it up to an a because the tests and final are really hard so with the curve you can get well over a 100%, but that would be extremely difficult to do.

So in the likelihood that I get my second b in precalc, does that drastically hurt my chances of getting into schools like Stanford and Caltech? If I manage to pull off straight As the rest of my high school career with taking challenging AP classes will it balance out? Since I’m only a sophomore I haven’t taken the SATs yet, but I got a 240 on my PSATs and I know that I can manage to get a 2400 on the SATs if I spend time this summer preparing.

What are ways that I can compensate for my two Bs in order to have a decent chance of getting into the top colleges? I’m planning on majoring in computer science, so my top choices are schools like Carnegie Mellon, Caltech, Stanford, Berkeley and so on, but what are some safety schools that have really good computer science programs for if I don’t get into any of those?

You definitely still have a chance, GPA is only one part of the application. My advice is to score highly on your SAT/ACT. Take SAT subject tests in math and science. If you score well on those, it will balance out your B and colleges will see that your high school was difficult. You can also do extracurriculars related to computer science to show more interest and dedication to your major.

Caltech is stats heavy so your chances there will be hurt more than stanford

you can compensate by having national/international level awards, good SAT (which should happen given your 240 psat), and amazing essays/LOR